Skip to content

Commit

Permalink
Update left_hand and cursor stack sync
Browse files Browse the repository at this point in the history
  • Loading branch information
mlus-asuka committed Oct 11, 2024
1 parent a77fc52 commit e22b21c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false

mod_version=1.20.1-1.3.3
mod_version=1.20.1-1.3.4
24 changes: 24 additions & 0 deletions src/main/java/vip/fubuki/playersync/PlayerSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import vip.fubuki.playersync.sync.VanillaSync;
import vip.fubuki.playersync.util.JDBCsetUp;

import java.sql.ResultSet;
import java.sql.SQLException;

@Mod(PlayerSync.MODID)
Expand Down Expand Up @@ -51,6 +52,8 @@ public void onServerStarting(ServerStartingEvent event) throws SQLException {
`advancements` blob,
`enderchest` mediumblob,
`effects` blob,
`left_hands` blob,
`cursors` blob,
`xp` int DEFAULT NULL,
`food_level` int DEFAULT NULL,
`score` int DEFAULT NULL,
Expand All @@ -59,6 +62,27 @@ public void onServerStarting(ServerStartingEvent event) throws SQLException {
`last_server` int DEFAULT NULL,
PRIMARY KEY (`uuid`)
);""");

JDBCsetUp.QueryResult queryResult = JDBCsetUp.executeQuery("""
SELECT COUNT(*) AS column_count
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'player_data';
""");

ResultSet resultSet = queryResult.resultSet();
int columnCount = 0;
if(resultSet.next()) {
columnCount = resultSet.getInt("column_count");
}

if(columnCount<14){
JDBCsetUp.executeUpdate("""
ALTER TABLE player_data
ADD COLUMN left_hand blob,
ADD COLUMN cursor blob;
""");
}

JDBCsetUp.executeUpdate("""
CREATE TABLE IF NOT EXISTS server_info (
`id` INT NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/vip/fubuki/playersync/sync/ModsSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void onPlayerJoin(Player player) throws SQLException {
Curios Support
*/
LazyOptional<top.theillusivec4.curios.api.type.capability.ICuriosItemHandler> itemHandler = top.theillusivec4.curios.api.CuriosApi.getCuriosInventory(player);
JDBCsetUp.QueryResult queryResult=JDBCsetUp.executeQuery("SELECT curios_item FROM curios WHERE uuid = '"+player.getUUID()+"'");
JDBCsetUp.QueryResult queryResult = JDBCsetUp.executeQuery("SELECT curios_item FROM curios WHERE uuid = '"+player.getUUID()+"'");
ResultSet resultSet = queryResult.resultSet();
if(resultSet.next()) {
String curios_data=resultSet.getString("curios_item");
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/vip/fubuki/playersync/sync/VanillaSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.nbt.NbtUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.player.Inventory;
Expand Down Expand Up @@ -83,6 +84,10 @@ public static void doPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) throws SQ
serverPlayer.experienceProgress=0;
serverPlayer.giveExperiencePoints(resultSet.getInt("xp"));
serverPlayer.setScore(resultSet.getInt("score"));
//Left Hand
serverPlayer.setItemInHand(InteractionHand.OFF_HAND,ItemStack.of(NbtUtils.snbtToStructure(resultSet.getString("left_hand").replace("|",",").replace("^","\"").replace("<","{").replace(">","}").replace("~", "'"))));
//Cursor
serverPlayer.containerMenu.setCarried(ItemStack.of(NbtUtils.snbtToStructure(resultSet.getString("cursors").replace("|",",").replace("^","\"").replace("<","{").replace(">","}").replace("~", "'"))));
//Equipment
String armor_data=resultSet.getString("armor");
if(armor_data.length()>2) {
Expand Down Expand Up @@ -212,6 +217,10 @@ public static void store(Player player, boolean init, boolean isServer) throws S
int score=player.getScore();
int food_level=player.getFoodData().getFoodLevel();
int health=(int) player.getHealth();
//Left hand
String left_hand = serialize(player.getItemInHand(InteractionHand.OFF_HAND).serializeNBT().toString());
//Cursor
String cursors = serialize(player.containerMenu.getCarried().serializeNBT().toString());
//Equipment
Map<Integer,String> equipment =new HashMap<>() ;
for (int i = 0; i < player.getInventory().armor.size(); i++) {
Expand Down Expand Up @@ -266,8 +275,8 @@ public static void store(Player player, boolean init, boolean isServer) throws S

//SQL Operation
if(init){
JDBCsetUp.executeUpdate("INSERT INTO player_data (uuid,armor,inventory,enderchest,advancements,effects,xp,food_level,health,score,online) VALUES ('"+player_uuid+"','"+equipment+"','"+inventoryMap+"','"+ender_chest+"','"+advancements+"','"+effectMap+"','"+XP+"','"+food_level+"','"+health+"','"+score+"',online=true)");
}else JDBCsetUp.executeUpdate("UPDATE player_data SET inventory = '"+inventoryMap+"',armor='"+equipment+"' ,xp='"+XP+"',effects='"+effectMap+"',enderchest='"+ender_chest+"',score='"+score+"',food_level='"+food_level+"',health='"+health+"',advancements='"+json+"' WHERE uuid = '"+player_uuid+"'");
JDBCsetUp.executeUpdate("INSERT INTO player_data (uuid,armor,inventory,enderchest,advancements,effects,xp,food_level,health,score,left_hand,cursors,online) VALUES ('"+player_uuid+"','"+equipment+"','"+inventoryMap+"','"+ender_chest+"','"+advancements+"','"+effectMap+"','"+XP+"','"+food_level+"','"+health+"','"+score+"','"+left_hand+"','"+cursors+"',online=true)");
}else JDBCsetUp.executeUpdate("UPDATE player_data SET inventory = '"+inventoryMap+"',armor='"+equipment+"' ,xp='"+XP+"',effects='"+effectMap+"',enderchest='"+ender_chest+"',score='"+score+"',food_level='"+food_level+"',health='"+health+"',advancements='"+json+"',left_hand='"+left_hand+"',cursors='"+cursors+"' WHERE uuid = '"+player_uuid+"'");
}

private static File[] scanAdvancementsFile(String player_uuid, File gameDir) {
Expand Down

0 comments on commit e22b21c

Please sign in to comment.