Add the gamemode inventory backup function

This commit is contained in:
zontreck 2023-12-18 18:17:02 -07:00
parent 750bc86550
commit 749c1964ad
75 changed files with 8656 additions and 7 deletions

View file

@ -6,9 +6,7 @@ import net.minecraft.core.BlockPos;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.eventbus.api.IEventBus;
@ -78,6 +76,34 @@ public class ModBlocks {
public static final RegistryObject<Item> STABLE_SINGULARITY_I = CreativeModeTabs.addToOTEModTab(ITEMS.register("stable_singularity", ()->new BlockItem(STABLE_SINGULARITY.get(), new Item.Properties())));
public static final RegistryObject<Block> LIMINAL_TILES = BLOCKS.register("liminal_tiles", ()-> new Block(BlockBehaviour.Properties.copy(Blocks.BEDROCK)));
public static final RegistryObject<Item> LIMINAL_TILES_I = CreativeModeTabs.addToOTEModTab(ITEMS.register("liminal_tiles", ()->new BlockItem(LIMINAL_TILES.get(), new Item.Properties())));
public static final RegistryObject<Block> BLACK = BLOCKS.register("black", ()->new Block(BlockBehaviour.Properties.copy(Blocks.BEDROCK)));
public static final RegistryObject<Item> BLOCK_I = CreativeModeTabs.addToOTEModTab(ITEMS.register("black", ()->new FoiledBlockItem(BLACK.get(), new Item.Properties().stacksTo(128))));
public static final RegistryObject<Block> LIMINAL_TILE_STAIRS = BLOCKS.register("liminal_tile_stairs", ()->new StairBlock(LIMINAL_TILES.get()::defaultBlockState, BlockBehaviour.Properties.copy(Blocks.BEDROCK).destroyTime(1000).strength(1000)));
public static final RegistryObject<Item> LIMINAL_TILE_STAIRS_I = CreativeModeTabs.addToOTEModTab(ITEMS.register("liminal_tile_stairs", ()->new BlockItem(LIMINAL_TILE_STAIRS.get(), new Item.Properties())));
public static final RegistryObject<Block> LIMINAL_TILE_SLAB = BLOCKS.register("liminal_tile_slab", ()->new SlabBlock(BlockBehaviour.Properties.copy(Blocks.BEDROCK)));
public static final RegistryObject<Item> LIMINAL_TILE_SLAB_I = CreativeModeTabs.addToOTEModTab(ITEMS.register("liminal_tile_slab", ()->new BlockItem(LIMINAL_TILE_SLAB.get(), new Item.Properties())));
public static final RegistryObject<Block> LIMINAL_WINDOW = BLOCKS.register("liminal_window", () -> new ParallaxWindow(15));
public static final RegistryObject<Item> LIMINAL_WINDOW_I = CreativeModeTabs.addToOTEModTab(ITEMS.register("liminal_window", () -> new BlockItem(LIMINAL_WINDOW.get(), new Item.Properties())));
/*
public static final class_2248 LIMINAL_WINDOW_NOON = (class_2248)createBlock("liminal_window_noon", new LiminalWindowBlock(QuiltBlockSettings.method_9630((class_4970)class_2246.field_31037).method_9626(class_2498.field_11537)), true, (class_1761)ModItems.LIMINAL_POOLS_ITEM_GROUP);
public static final class_2248 LIMINAL_WINDOW_DUSK = (class_2248)createBlock("liminal_window_dusk", new LiminalWindowBlock(QuiltBlockSettings.method_9630((class_4970)class_2246.field_31037).method_9626(class_2498.field_11537)), true, (class_1761)ModItems.LIMINAL_POOLS_ITEM_GROUP);
public static final class_2248 LIMINAL_WINDOW_ABYSS = (class_2248)createBlock("liminal_window_abyss", new LiminalWindowBlock(QuiltBlockSettings.method_9630((class_4970)class_2246.field_31037).method_9626(class_2498.field_11537)), true, (class_1761)ModItems.LIMINAL_POOLS_ITEM_GROUP);*/
private static boolean never(BlockState p_50806_, BlockGetter p_50807_, BlockPos p_50808_) {

View file

@ -0,0 +1,30 @@
package dev.zontreck.otemod.blocks;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
public class ParallaxWindow extends BaseEntityBlock
{
protected static final double OFFSET = 0.0625;
public ParallaxWindow(int light) {
super(Properties.copy(Blocks.BEDROCK).lightLevel(x->light).sound(SoundType.GLASS));
}
@Override
public boolean propagatesSkylightDown(BlockState pState, BlockGetter pReader, BlockPos pPos) {
return true;
}
@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
return null;
}
}

View file

@ -2,6 +2,7 @@ package dev.zontreck.otemod.blocks.entity;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.blocks.ModBlocks;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
@ -16,6 +17,10 @@ public class ModEntities {
public static final RegistryObject <BlockEntityType <MagicalScrubberBlockEntity>> MAGICAL_SCRUBBER = ENTITIES.register("magical_scrubber", ()-> BlockEntityType.Builder.of(MagicalScrubberBlockEntity::new, ModBlocks.MAGICAL_SCRUBBER.get()).build(null));
public static final ResourceLocation PARALLAX_BLOCK = new ResourceLocation(OTEMod.MOD_ID, "parallax_block");
public static final RegistryObject <BlockEntityType <ParallaxWindowEntity>> PARALLAX_WINDOW_ENTITY = ENTITIES.register("parallax_window", ()->BlockEntityType.Builder.of(ParallaxWindowEntity::new, ModBlocks.LIMINAL_WINDOW.get()).build(null));
public static void register(IEventBus eventBus)
{

View file

@ -0,0 +1,62 @@
package dev.zontreck.otemod.blocks.entity;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.joml.Matrix4f;
import java.util.Arrays;
public class ParallaxWindowEntity extends BlockEntity
{
public enum SkyType
{
Level1
}
public ParallaxWindowEntity(BlockPos pPos, BlockState pBlockState) {
super(ModEntities.PARALLAX_WINDOW_ENTITY.get(), pPos, pBlockState);
}
private SkyType skyType = SkyType.Level1;
public SkyType getSkyType() {
return skyType;
}
@Override
protected void saveAdditional(CompoundTag compoundTag) {
compoundTag.putString("skyType", this.skyType.name());
}
@Override
public void load(CompoundTag compoundTag) {
if (!compoundTag.contains("skyType")) {
return;
}
this.skyType = SkyType.valueOf(compoundTag.getString("skyType"));
}
private final Boolean[] shouldRender = new Boolean[6];
public boolean shouldRenderFace(Direction direction) {
int index = direction.ordinal();
if (shouldRender[index] == null) {
shouldRender[index] = level == null || Block.shouldRenderFace(getBlockState(), level, getBlockPos(), direction, getBlockPos().relative(direction));
}
return shouldRender[index];
}
public void neighborChanged() {
Arrays.fill(shouldRender, null);
}
}

View file

@ -0,0 +1,49 @@
package dev.zontreck.otemod.client.renderer;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import dev.zontreck.otemod.blocks.ParallaxWindow;
import dev.zontreck.otemod.blocks.entity.ParallaxWindowEntity;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.core.Direction;
import org.joml.Matrix4f;
public class ParallaxWindowEntityRenderer implements BlockEntityRenderer<ParallaxWindowEntity>
{
@Override
public void render(ParallaxWindowEntity parallaxWindowEntity, float v, PoseStack poseStack, MultiBufferSource multiBufferSource, int i, int i1) {
Matrix4f m4f = poseStack.last().pose();
var renderType = switch (parallaxWindowEntity.getSkyType()) {
case Level1 -> RenderType.endPortal();
};
renderCube(parallaxWindowEntity, m4f, multiBufferSource.getBuffer(renderType));
}
private void renderCube(ParallaxWindowEntity entity, Matrix4f matrix, VertexConsumer buffer) {
renderFace(entity, matrix, buffer, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, Direction.SOUTH);
renderFace(entity, matrix, buffer, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, Direction.NORTH);
renderFace(entity, matrix, buffer, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, Direction.EAST);
renderFace(entity, matrix, buffer, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, Direction.WEST);
renderFace(entity, matrix, buffer, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, Direction.DOWN);
renderFace(entity, matrix, buffer, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, Direction.UP);
}
private void renderFace(ParallaxWindowEntity entity, Matrix4f matrix, VertexConsumer buffer, float f, float g, float h, float i, float j, float k, float l, float m, Direction direction) {
if (entity.shouldRenderFace(direction)) {
buffer.vertex(matrix, f, h, j).endVertex();
buffer.vertex(matrix, g, h, k).endVertex();
buffer.vertex(matrix, g, i, l).endVertex();
buffer.vertex(matrix, f, i, m).endVertex();
}
}
@Override
public int getViewDistance() {
return 256;
}
}

View file

@ -12,6 +12,7 @@ import dev.zontreck.otemod.configs.OTEServerConfig;
import dev.zontreck.otemod.enchantments.MobEggEnchantment;
import dev.zontreck.otemod.enchantments.ModEnchantments;
import dev.zontreck.otemod.implementation.DeathMessages;
import dev.zontreck.otemod.implementation.InventoryBackup;
import dev.zontreck.otemod.items.tags.ItemStatType;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
@ -22,11 +23,14 @@ import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.GameType;
import net.minecraftforge.common.ForgeSpawnEggItem;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.items.ItemStackHandler;
import java.time.Instant;
import java.util.Date;
@ -137,5 +141,24 @@ 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

@ -21,7 +21,12 @@ public class DeathMessages {
messages.add("!Dark_Red![1]!Dark_Red! experienced their [0] death, while running away in fear from [2]");
messages.add("!Dark_Red![1] was eaten alive by [2]");
messages.add("!Dark_Red!For their [0] death, [1]!Dark_Red! got a little bit too careless!");
messages.add("!Dark_Red!Not all whimpering messes are good, as [1]!Dark_Red! is evidence of.");
messages.add("!Dark_Red![0]!? Seriously?! Come on! [1]!Dark_Red! you can do better than this!");
messages.add("!Dark_Red!What is that... the [0]th time? For fucks sake, I'm not even surprised anymore.");
messages_falling.add("!Dark_Red![1]!Dark_Red!... oh my dear sweet [1]!Dark_Red!, oh you sweet summer child with ribbons in your hair... you cannot in fact, fly, (yet)");
messages_falling.add("!Dark_Red![1]!Dark_Red! clearly had the delusion they could fly.... their [0] death says otherwise.");
messages_falling.add("!Dark_Red![1]!Dark_Red! tried flying, but forgot their wings");
}

View file

@ -0,0 +1,74 @@
package dev.zontreck.otemod.implementation;
import dev.zontreck.otemod.database.OTEDatastore;
import net.minecraft.nbt.*;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.GameType;
import net.minecraftforge.items.ItemStackHandler;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public class InventoryBackup extends OTEDatastore
{
ServerPlayer player;
File my_file;
ListTag list;
public static final Path FILE_TREE_PATH = of("gamemode_inventories");
public InventoryBackup(ServerPlayer player, GameType mode)
{
this.player = player;
var temp = FILE_TREE_PATH.resolve(player.getStringUUID());
if(!temp.toFile().exists()) temp.toFile().mkdir();
my_file = temp.resolve(mode.getName() + ".nbt").toFile();
}
public void restore()
{
try {
CompoundTag tag = NbtIo.read(my_file);
list = tag.getList("inventory", Tag.TAG_COMPOUND);
} catch (IOException e) {
e.printStackTrace();
}
}
public void save()
{
try{
CompoundTag tag = new CompoundTag();
list.clear();
list = player.getInventory().save(list);
tag.put("inventory", list);
NbtIo.write(tag, my_file);
}catch(Exception e)
{
e.printStackTrace();
}
}
public void apply()
{
try {
player.getInventory().load(list);
}catch(Exception e)
{
e.printStackTrace();
}
}
}

View file

@ -1,6 +1,7 @@
package dev.zontreck.otemod.items;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.blocks.FoiledBlockItem;
import dev.zontreck.otemod.entities.ModEntityTypes;
import dev.zontreck.otemod.implementation.CreativeModeTabs;
import net.minecraft.world.item.CreativeModeTab;
@ -52,6 +53,8 @@ public class ModItems {
public static final RegistryObject<Item> VAULT_RAW_ORE = CreativeModeTabs.addToOTEModTab(ITEMS.register("raw_vault_steel_ore", () -> new Item(new Item.Properties().stacksTo(64))));
public static final RegistryObject<Item> POSS_BALL = CreativeModeTabs.addToOTEModTab(ITEMS.register("poss_ball", () -> new PossBallItem(new Item.Properties())));
//public static final RegistryObject<Item> POSSUM_SPAWN_EGG = ITEMS.register("possum_spawn_egg", () -> new ForgeSpawnEggItem(ModEntityTypes.POSSUM, 0x938686, 0xc68787, new Item.Properties())));

View file

@ -0,0 +1,27 @@
package dev.zontreck.otemod.items;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
public class PossBallItem extends Item
{
public PossBallItem(Properties pProperties) {
super(pProperties);
}
public boolean contents=false;
@Override
public boolean isFoil(ItemStack pStack) {
return contents;
}
@Override
public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pUsedHand) {
contents = !contents;
return super.use(pLevel, pPlayer, pUsedHand);
}
}