Get the new feature working

This commit is contained in:
zontreck 2023-12-18 18:39:07 -07:00
parent 749c1964ad
commit df1c1f73ba
5 changed files with 34 additions and 21 deletions

View file

@ -49,7 +49,7 @@ mod_name=OTEMod
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPLv3 mod_license=GPLv3
# The mod version. See https://semver.org/ # The mod version. See https://semver.org/
mod_version=1.4.121623.1553 mod_version=1.4.121823.1838
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources. # This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html # See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View file

@ -14,15 +14,19 @@ import com.mojang.logging.LogUtils;
import dev.zontreck.libzontreck.chat.ChatColor; import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.vectors.Vector3; import dev.zontreck.libzontreck.vectors.Vector3;
import dev.zontreck.otemod.implementation.CreativeModeTabs; import dev.zontreck.otemod.implementation.CreativeModeTabs;
import dev.zontreck.otemod.implementation.InventoryBackup;
import dev.zontreck.otemod.integrations.KeyBindings; import dev.zontreck.otemod.integrations.KeyBindings;
import net.minecraft.client.gui.screens.MenuScreens; import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.renderer.entity.EntityRenderers; import net.minecraft.client.renderer.entity.EntityRenderers;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.GameType;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.ModLoadingContext;
@ -126,6 +130,24 @@ public class OTEMod
ModMessages.register(); ModMessages.register();
} }
@SubscribeEvent
public void onGameModeChanged(PlayerEvent.PlayerChangeGameModeEvent event)
{
ServerPlayer player = (ServerPlayer) event.getEntity();
InventoryBackup backup = new InventoryBackup(player, event.getCurrentGameMode());
InventoryBackup restore = new InventoryBackup(player, event.getNewGameMode());
restore.restore();
backup.save();
if(event.getNewGameMode() == GameType.CREATIVE)
{
player.getInventory().clearContent();
return;
}
restore.apply();
}
public boolean firstJoin(Player p){ public boolean firstJoin(Player p){

View file

@ -142,23 +142,5 @@ public class EventHandler {
} }
@SubscribeEvent
public void onGameModeChanged(PlayerEvent.PlayerChangeGameModeEvent event)
{
ServerPlayer player = (ServerPlayer) event.getEntity();
InventoryBackup backup = new InventoryBackup(player, event.getCurrentGameMode());
InventoryBackup restore = new InventoryBackup(player, event.getNewGameMode());
restore.restore();
backup.save();
if(event.getNewGameMode() == GameType.CREATIVE)
{
player.getInventory().clearContent();
return;
}
restore.apply();
}
} }

View file

@ -25,6 +25,10 @@ public class InventoryBackup extends OTEDatastore
public InventoryBackup(ServerPlayer player, GameType mode) public InventoryBackup(ServerPlayer player, GameType mode)
{ {
this.player = player; this.player = player;
if(!FILE_TREE_PATH.toFile().exists())
{
FILE_TREE_PATH.toFile().mkdir();
}
var temp = FILE_TREE_PATH.resolve(player.getStringUUID()); var temp = FILE_TREE_PATH.resolve(player.getStringUUID());
if(!temp.toFile().exists()) temp.toFile().mkdir(); if(!temp.toFile().exists()) temp.toFile().mkdir();
@ -34,6 +38,11 @@ public class InventoryBackup extends OTEDatastore
public void restore() public void restore()
{ {
try { try {
if(!my_file.exists())
{
list = new ListTag();
return;
}
CompoundTag tag = NbtIo.read(my_file); CompoundTag tag = NbtIo.read(my_file);
list = tag.getList("inventory", Tag.TAG_COMPOUND); list = tag.getList("inventory", Tag.TAG_COMPOUND);
@ -48,7 +57,7 @@ public class InventoryBackup extends OTEDatastore
{ {
try{ try{
CompoundTag tag = new CompoundTag(); CompoundTag tag = new CompoundTag();
list.clear(); list = new ListTag();
list = player.getInventory().save(list); list = player.getInventory().save(list);
tag.put("inventory", list); tag.put("inventory", list);

View file

@ -14,7 +14,7 @@
} }
}, },
"result": { "result": {
"item": "otemod:poss_ball", // private obtaining security system "item": "otemod:poss_ball",
"count": 1 "count": 1
} }
} }