Adds in item scrubber
Fixes vault item highlight issue with new vault GUI
Begin adding some functions from Tinkers Construct, as well as assets.
Add a nether resource dimension
This commit is contained in:
Tara 2023-01-16 18:22:40 -07:00
parent f3bce6751b
commit 2a3fec5d66
140 changed files with 2984 additions and 215 deletions

View file

@ -0,0 +1,109 @@
package dev.zontreck.otemod.implementation.vault;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.chat.ChatServerOverride;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.inventory.MenuConstructor;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.ItemStackHandler;
public class VaultContainer
{
public static Map<UUID, VaultContainer> VAULT_REGISTRY = new HashMap<>();
public VaultMenu theContainer;
public ItemStackHandler myInventory;
public MenuConstructor serverMenu;
public UUID owner;
private MinecraftServer server;
private final int VAULT_NUMBER;
public final UUID VaultID;
public VaultContainer(ServerPlayer player, int vaultNum) {
myInventory = new ItemStackHandler(54); // Vaults have a fixed size at the same as a double chest
theContainer = new VaultMenu(player.containerCounter+1, player.getInventory(), myInventory, BlockPos.ZERO);
VaultID = theContainer.VaultMenuID;
owner = player.getUUID();
server=player.server;
serverMenu = theContainer.getServerMenu(myInventory);
VAULT_NUMBER=vaultNum;
if(VAULT_NUMBER == -1)return; // Trash ID
Connection con = OTEMod.DB.getConnection();
// Check database for vault
PreparedStatement pstat;
try {
con.beginRequest();
pstat = con.prepareStatement("SELECT * FROM `vaults` WHERE `uuid`=? AND `number`=?;");
pstat.setString(1, player.getStringUUID());
pstat.setInt(2, vaultNum);
ResultSet rs = pstat.executeQuery();
while(rs.next())
{
// We have a vault, deserialize the container
String data = rs.getString("data");
CompoundTag inv = NbtUtils.snbtToStructure(data);
myInventory.deserializeNBT(inv);
}
con.endRequest();
} catch (SQLException | CommandSyntaxException e) {
e.printStackTrace();
}
// Container is now ready to be sent to the client!
}
public void commit()
{
if(VAULT_NUMBER == -1)return; // We have no need to save the trash
CompoundTag saved = myInventory.serializeNBT();
ChatServerOverride.broadcastToAbove(owner, Component.literal(ChatColor.BOLD+ChatColor.DARK_GREEN+"Saving the vault's contents..."), server);
String toSave= NbtUtils.structureToSnbt(saved);
Connection con = OTEMod.DB.getConnection();
try{
con.beginRequest();
PreparedStatement ps = con.prepareStatement("REPLACE INTO `vaults` (uuid, number, data) VALUES (?,?,?);");
ps.setString(1, owner.toString());
ps.setInt(2, VAULT_NUMBER);
ps.setString(3, toSave);
boolean has_items = false;
for (int i = 0; i< myInventory.getSlots(); i++){
ItemStack IS = myInventory.getStackInSlot(i);
if(!IS.isEmpty()){
has_items=true;
}
}
if(!has_items)
{
ps = con.prepareStatement("DELETE FROM `vaults` WHERE uuid=? AND number=?;");
ps.setString(1, owner.toString());
ps.setInt(2, VAULT_NUMBER);
}
ps.execute();
con.endRequest();
}catch(SQLException e){
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,147 @@
package dev.zontreck.otemod.implementation.vault;
import java.util.Map;
import java.util.UUID;
import dev.zontreck.otemod.implementation.inits.ModMenuTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuConstructor;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.items.SlotItemHandler;
public class VaultMenu extends AbstractContainerMenu
{
//private final ContainerLevelAccess containerAccess;
public final UUID VaultMenuID;
public VaultMenu (int id, Inventory player)
{
this(id, player, new ItemStackHandler(54), BlockPos.ZERO);
}
public VaultMenu (int id, Inventory player, IItemHandler slots, BlockPos pos)
{
super(ModMenuTypes.VAULT.get(), id);
VaultMenuID=UUID.randomUUID();
//this.containerAccess = ContainerLevelAccess.create(player.player.level, pos);
final int slotSize = 18;
final int startX = 24;
final int inventoryY = 38;
addPlayerInventory(player);
addPlayerHotbar(player);
for (int row = 0; row < 6; row++)
{
for (int column = 0; column < 9; column++)
{
addSlot(new SlotItemHandler(slots, row*9 + column, startX+column * slotSize , inventoryY + row * slotSize));
}
}
}
// CREDIT GOES TO: diesieben07 | https://github.com/diesieben07/SevenCommons
// must assign a slot number to each of the slots used by the GUI.
// For this container, we can see both the tile inventory's slots as well as the player inventory slots and the hotbar.
// Each time we add a Slot to the container, it automatically increases the slotIndex, which means
// 0 - 8 = hotbar slots (which will map to the InventoryPlayer slot numbers 0 - 8)
// 9 - 35 = player inventory slots (which map to the InventoryPlayer slot numbers 9 - 35)
// 36 - 44 = TileInventory slots, which map to our TileEntity slot numbers 0 - 8)
private static final int HOTBAR_SLOT_COUNT = 9;
private static final int PLAYER_INVENTORY_ROW_COUNT = 3;
private static final int PLAYER_INVENTORY_COLUMN_COUNT = 9;
private static final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT;
private static final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT;
private static final int VANILLA_FIRST_SLOT_INDEX = 0;
private static final int TE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT;
// THIS YOU HAVE TO DEFINE!
private static final int TE_INVENTORY_SLOT_COUNT = 54; // must be the number of slots you have!
@Override
public ItemStack quickMoveStack(Player playerIn, int index) {
Slot sourceSlot = slots.get(index);
if (sourceSlot == null || !sourceSlot.hasItem()) return ItemStack.EMPTY; //EMPTY_ITEM
ItemStack sourceStack = sourceSlot.getItem();
ItemStack copyOfSourceStack = sourceStack.copy();
// Check if the slot clicked is one of the vanilla container slots
if (index < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) {
// This is a vanilla container slot so merge the stack into the tile inventory
if (!moveItemStackTo(sourceStack, TE_INVENTORY_FIRST_SLOT_INDEX, TE_INVENTORY_FIRST_SLOT_INDEX
+ TE_INVENTORY_SLOT_COUNT, false)) {
return ItemStack.EMPTY; // EMPTY_ITEM
}
} else if (index < TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT) {
// This is a TE slot so merge the stack into the players inventory
if (!moveItemStackTo(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) {
return ItemStack.EMPTY;
}
} else {
System.out.println("Invalid slotIndex:" + index);
return ItemStack.EMPTY;
}
// If stack size == 0 (the entire stack was moved) set slot contents to null
if (sourceStack.getCount() == 0) {
sourceSlot.set(ItemStack.EMPTY);
} else {
sourceSlot.setChanged();
}
sourceSlot.onTake(playerIn, sourceStack);
return copyOfSourceStack;
}
@Override
public boolean stillValid(Player p_38874_) {
return true; // We have no block
}
public static MenuConstructor getServerMenu (ItemStackHandler inventory){
return (id, player, play) -> new VaultMenu(id, player, inventory, BlockPos.ZERO);
}
public void doCommitAction()
{
// Locate the Vault in the Vault Registry and commit changes.
// Search for myself!
for(Map.Entry<UUID,VaultContainer> e : VaultContainer.VAULT_REGISTRY.entrySet())
{
if(e.getValue().VaultID.equals(VaultMenuID))
{
e.getValue().commit();
}
}
}
private static final int PLAYER_INVENTORY_FIRST_SLOT_HEIGHT = 156;
private static final int PLAYER_INVENTORY_FIRST_SLOT_LEFT = 24;
private static final int PLAYER_HOTBAR_FIRST_SLOT = 212;
private void addPlayerInventory(Inventory inv)
{
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 9; j++) {
this.addSlot(new Slot(inv, j+i*9+9, PLAYER_INVENTORY_FIRST_SLOT_LEFT+j*18, PLAYER_INVENTORY_FIRST_SLOT_HEIGHT+i*18));
}
}
}
private void addPlayerHotbar(Inventory inv)
{
for (int index = 0; index < 9; index++) {
this.addSlot(new Slot(inv, index, PLAYER_INVENTORY_FIRST_SLOT_LEFT+index*18, PLAYER_HOTBAR_FIRST_SLOT));
}
}
}

View file

@ -0,0 +1,68 @@
package dev.zontreck.otemod.implementation.vault;
import java.util.UUID;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import dev.zontreck.otemod.OTEMod;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
public class VaultScreen extends AbstractContainerScreen <VaultMenu>
{
// 176x224
public final UUID VaultMenuID;
private static final ResourceLocation TEXTURE = new ResourceLocation(OTEMod.MOD_ID, "textures/gui/vault.png");
public VaultScreen(VaultMenu container, Inventory playerInv, Component comp){
super(container, playerInv, comp);
this.VaultMenuID = container.VaultMenuID;
this.leftPos = 0;
this.topPos = 0;
this.imageWidth = 207;
this.imageHeight = 238;
}
@Override
public void render(PoseStack stack, int mouseX, int mouseY, float partialTicks)
{
this.renderBackground(stack);
super.render(stack, mouseX, mouseY, partialTicks);
this.renderTooltip(stack, mouseX, mouseY);
}
@Override
protected void renderLabels(PoseStack stack, int mouseX, int mouseY)
{
this.font.draw(stack, this.title, 63, 12, 0xFFFFFF);
this.font.draw(stack, this.playerInventoryTitle, 63, 146, 0xFFFFFF);
//this.font.draw(stack, this.title.getString(), this.leftPos + 17, this.topPos + 15, 0xFFFFFF);
//this.font.draw(stack, this.playerInventoryTitle.getString(), this.leftPos + 17, this.topPos + 123, 0xFFFFFF);
}
@Override
protected void init()
{
super.init();
// This is where custom controls would be added!
}
@Override
protected void renderBg(PoseStack stack, float mouseX, int mouseY, int partialTicks)
{
renderBackground(stack);
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor (1.0f, 1.0f, 1.0f, 1.0f);
RenderSystem.setShaderTexture(0, TEXTURE);
blit(stack, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight);
}
}

View file

@ -0,0 +1,34 @@
package dev.zontreck.otemod.implementation.vault;
import java.util.Map;
import java.util.UUID;
import dev.zontreck.otemod.OTEMod;
import net.minecraftforge.event.entity.player.PlayerContainerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
@EventBusSubscriber(modid=OTEMod.MOD_ID,bus=Mod.EventBusSubscriber.Bus.FORGE)
public class VaultWatcher {
@SubscribeEvent
public void onClosedContainer(PlayerContainerEvent.Close ev)
{
if(ev.getEntity().level.isClientSide)return;
//OTEMod.LOGGER.info("Player closed a container");
// Player closed the container
// Check if it is a vault Container
if(ev.getContainer() instanceof VaultMenu)
{
// During testing the instance of VaultMenu we get passed back through this method gets a regenerated Vault ID, so our only option is to iterate here and commit a vault based on owner ID
for(Map.Entry<UUID, VaultContainer> entry : VaultContainer.VAULT_REGISTRY.entrySet()){
if(entry.getKey() == ev.getEntity().getUUID())
{
entry.getValue().commit();
}
}
}
}
}