Adds a starter kit system

This commit is contained in:
zontreck 2023-12-20 17:04:12 -07:00
parent 6f0511990e
commit 97e13a1e17
16 changed files with 714 additions and 20 deletions

View file

@ -3,7 +3,7 @@
org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false
libzontreck=1.9.121823.1318
libzontreck=1.9.122023.1612
## Environment Properties

View file

@ -12,18 +12,26 @@ import java.util.Set;
import com.mojang.logging.LogUtils;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.profiles.Profile;
import dev.zontreck.libzontreck.profiles.UserProfileNotYetExistsException;
import dev.zontreck.libzontreck.util.ChatHelpers;
import dev.zontreck.libzontreck.vectors.Vector3;
import dev.zontreck.otemod.implementation.CreativeModeTabs;
import dev.zontreck.otemod.implementation.InventoryBackup;
import dev.zontreck.otemod.implementation.Messages;
import dev.zontreck.otemod.implementation.PlayerFirstJoinTag;
import dev.zontreck.otemod.implementation.vault.*;
import dev.zontreck.otemod.integrations.KeyBindings;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.renderer.entity.EntityRenderers;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.commands.GiveCommand;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.GameType;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerEvent;
@ -39,6 +47,7 @@ import net.minecraftforge.event.server.ServerStartedEvent;
import net.minecraftforge.event.server.ServerStoppingEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.items.ItemStackHandler;
import org.slf4j.Logger;
import dev.zontreck.otemod.blocks.ModBlocks;
@ -53,8 +62,6 @@ import dev.zontreck.otemod.events.LoreHandlers;
import dev.zontreck.otemod.implementation.inits.ModMenuTypes;
import dev.zontreck.otemod.implementation.scrubber.ItemScrubberScreen;
import dev.zontreck.otemod.implementation.scrubber.MagicalScrubberScreen;
import dev.zontreck.otemod.implementation.vault.VaultScreen;
import dev.zontreck.otemod.implementation.vault.VaultWatcher;
import dev.zontreck.otemod.items.ModItems;
//import dev.zontreck.otemod.ore.Modifier.ModifierOfBiomes;
import dev.zontreck.otemod.networking.ModMessages;
@ -66,7 +73,6 @@ public class OTEMod
public static final Vector3 ZERO_VECTOR = new Vector3(0,0,0);
// Directly reference a slf4j logger
public static final Logger LOGGER = LogUtils.getLogger();
public static final String FIRST_JOIN_TAG = "dev.zontreck.otemod.firstjoin";
public static final String MOD_ID = "otemod";
public static final String MODIFY_BIOMES = "modify_biomes";
public static final ResourceLocation MODIFY_BIOMES_RL = new ResourceLocation(OTEMod.MOD_ID, MODIFY_BIOMES);
@ -149,18 +155,51 @@ public class OTEMod
restore.apply();
}
public boolean firstJoin(Player p){
Set<String> tags = p.getTags();
public static void checkFirstJoin(ServerPlayer p){
try {
Profile prof = Profile.get_profile_of(p.getStringUUID());
if(tags.contains(OTEMod.FIRST_JOIN_TAG)){
return false;
PlayerFirstJoinTag tag = PlayerFirstJoinTag.load(prof.NBT);
if(tag == null)
{
tag = PlayerFirstJoinTag.now();
tag.save(prof.NBT);
}else {
Starter data = StarterProvider.getStarter();
if(data.getLastChanged() > tag.LastGiven && OTEServerConfig.GIVE_KIT_EVERY_CHANGE.get())
{
tag = PlayerFirstJoinTag.now();
tag.save(prof.NBT);
}else return;
}
prof.commit();
//p.addTag(OTEMod.FIRST_JOIN_TAG);
try {
ItemStackHandler startKit = StarterProvider.getStarter().getItems();
ChatHelpers.broadcastTo(p, ChatHelpers.macro(Messages.STARTER_KIT_GIVEN), p.server);
for(int i = 0;i<startKit.getSlots();i++)
{
if(i>=p.getInventory().getContainerSize())
{
break;
} else {
p.getInventory().add(startKit.getStackInSlot(i));
}
}
} catch (NoMoreVaultException e) {
throw new RuntimeException(e);
}
} catch (UserProfileNotYetExistsException e) {
throw new RuntimeException(e);
} catch (NoMoreVaultException e) {
throw new RuntimeException(e);
}
//p.addTag(ShapedAionResources.FIRST_JOIN_TAG);
return true;
}
// You can use SubscribeEvent and let the Event Bus discover methods to call

View file

@ -53,6 +53,8 @@ public class ChatServerOverride {
c.FlyEnabled = mayFly;
c.Assert(play);
OTEMod.checkFirstJoin(ev.player);
if(!OTEServerConfig.USE_CUSTOM_JOINLEAVE.get()) return;
ChatHelpers.broadcast(ChatHelpers.macro("!Dark_Gray![!Dark_Green!+!Dark_Gray!] !Bold!!Dark_Aqua![0]",prof.nickname), ev.level.getServer());

View file

@ -11,6 +11,7 @@ import dev.zontreck.otemod.commands.profilecmds.NameColorCommand;
import dev.zontreck.otemod.commands.profilecmds.NickCommand;
import dev.zontreck.otemod.commands.profilecmds.PrefixColorCommand;
import dev.zontreck.otemod.commands.profilecmds.PrefixCommand;
import dev.zontreck.otemod.commands.vaults.StarterCommand;
import dev.zontreck.otemod.commands.vaults.TrashCommand;
import dev.zontreck.otemod.commands.vaults.VaultCommand;
import dev.zontreck.otemod.commands.zschem.LoadSchem;
@ -104,5 +105,6 @@ public class CommandRegistry {
PlaceAsAir.register(ev.getDispatcher());
ShareItemInChatCommand.register(ev.getDispatcher());
StarterCommand.register(ev.getDispatcher());
}
}

View file

@ -0,0 +1,73 @@
package dev.zontreck.otemod.commands.vaults;
import com.mojang.brigadier.CommandDispatcher;
import dev.zontreck.libzontreck.util.ChatHelpers;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.configs.OTEServerConfig;
import dev.zontreck.otemod.implementation.Messages;
import dev.zontreck.otemod.implementation.vault.NoMoreVaultException;
import dev.zontreck.otemod.implementation.vault.StarterContainer;
import dev.zontreck.otemod.implementation.vault.VaultContainer;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraftforge.network.NetworkHooks;
import java.time.Instant;
public class StarterCommand
{
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("starter").executes(x->openStarterMenu(x.getSource())));
}
public static int openStarterMenu(CommandSourceStack ctx)
{
ServerPlayer player = ctx.getPlayer();
if(player != null)
{
if(player.hasPermissions(ctx.getServer().getOperatorUserPermissionLevel()))
{
try {
StarterContainer container = new StarterContainer(player);
NetworkHooks.openScreen(player, new SimpleMenuProvider(container.serverMenu, Component.literal("Starter Gear")));
// Add to the master vault registry
if(StarterContainer.VAULT_REGISTRY.containsKey(player.getUUID()))StarterContainer.VAULT_REGISTRY.remove(player.getUUID());
StarterContainer.VAULT_REGISTRY.put(player.getUUID(), container);
} catch (NoMoreVaultException e) {
throw new RuntimeException(e);
}
}else {
ChatHelpers.broadcastTo(player, ChatHelpers.macro(Messages.STARTER_FAILURE_PERMISSIONS), ctx.getServer());
}
return 0;
}
ctx.sendFailure(ChatHelpers.macro(Messages.STARTER_FAILURE_CONSOLE));
return 1;
}
public static void doOpen(ServerPlayer p){
StarterContainer container;
try {
container = new StarterContainer(p);
} catch (NoMoreVaultException e) {
ChatHelpers.broadcastTo(p.getUUID(), ChatHelpers.macro(OTEMod.OTEPrefix+"!Dark_Red!You cannot open anymore vaults. Craft a new vault!"), p.server);
return;
}
NetworkHooks.openScreen(p, new SimpleMenuProvider(container.serverMenu, Component.literal("Starter Gear")));
// Add to the master vault registry
if(StarterContainer.VAULT_REGISTRY.containsKey(p.getUUID()))VaultContainer.VAULT_REGISTRY.remove(p.getUUID());
StarterContainer.VAULT_REGISTRY.put(p.getUUID(), container);
}
}

View file

@ -11,7 +11,6 @@ public class OTEServerConfig {
public static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
public static final ForgeConfigSpec SPEC;
public static final ForgeConfigSpec.ConfigValue<List<ItemStack>> INITIAL_ITEMS_TO_GIVE_ON_FIRST_JOIN;
public static final ForgeConfigSpec.ConfigValue<Double> SPAWN_EGG_CHANCE;
public static final ForgeConfigSpec.ConfigValue<Integer> ITEM_DESPAWN_TIMER;
@ -36,15 +35,22 @@ public class OTEServerConfig {
public static final ForgeConfigSpec.ConfigValue<Boolean> ENABLE_DEATH_MESSAGES;
public static final ForgeConfigSpec.ConfigValue<Boolean> GIVE_KIT_EVERY_CHANGE;
static {
List<ItemStack> defaults = new ArrayList<ItemStack>();
List<String> defaultExcludeDimensions = new ArrayList<String>();
defaultExcludeDimensions.add("minecraft:the_nether"); // Excluded to make mining for Ancient Debris easier
defaultExcludeDimensions.add("minecraft:the_end"); // Excluded due to End Crystals
BUILDER.push("OTE");
INITIAL_ITEMS_TO_GIVE_ON_FIRST_JOIN = BUILDER.comment("What items, identified by modid:item, to give to a brand new user on the server").define("New Player Gear", defaults);
BUILDER.push("STARTERKIT");
GIVE_KIT_EVERY_CHANGE = BUILDER.comment("Enable this to give the starter kit every time it is changed, regardless of whether the player has already received that kit on their next join.").define("starter_kit_given_on_change", false);
BUILDER.pop();
MAX_VAULTS = BUILDER.comment("What is the maximum number of vaults a player may have available? (0 is unlimited)").define("max_vaults", 0);
ITEM_DESPAWN_TIMER = BUILDER.comment("How many times should the item's expire be cancelled. The vanilla expire time is 5 minutes, so this would be ticked down once every 5 minutes.").define("item_extra_lives", 2);
ENABLE_DEATH_MESSAGES = BUILDER.comment("Whether you want to enable the death messages to be output by OTEMod when a player dies. These can be quite random!").define("enable_ote_death_msgs", true);
@ -83,6 +89,7 @@ public class OTEServerConfig {
USE_CUSTOM_CHATREPLACER = BUILDER.comment("Whether to use the custom chat replacer (If disabled the relevant commands will be removed)").define("chatprettifier", true);
BUILDER.pop();
SPEC=BUILDER.build();
}
}

View file

@ -7,15 +7,21 @@ public class Messages {
public static final String NICK_UPDATED;
public static final String NAME_COLOR_UPDATED;
public static final String CHAT_COLOR_UPDATED;
public static final String STARTER_FAILURE_CONSOLE;
public static final String STARTER_FAILURE_PERMISSIONS;
public static final String STARTER_KIT_GIVEN;
static{
OTE_PREFIX = "!Dark_Gray![!Dark_Purple!OTE!Dark_Gray!] ";
PREFIX_UPDATED = OTE_PREFIX + " !Dark_Purple!Your prefix has been updated";
PREFIX_COLOR_UPDATED = OTE_PREFIX + "!Dark_Purple! Your prefix color has been updated";
PREFIX_COLOR_UPDATED = OTE_PREFIX + "!Dark_Purple!Your prefix color has been updated";
NICK_UPDATED = OTE_PREFIX + "!Dark_Purple! Your nickname has been updated";
NAME_COLOR_UPDATED = OTE_PREFIX + "!Dark_Purple! Your name color has been updated";
CHAT_COLOR_UPDATED = OTE_PREFIX + "!Dark_Purple! Your chat color has been updated";
NAME_COLOR_UPDATED = OTE_PREFIX + "!Dark_Purple!Your name color has been updated";
CHAT_COLOR_UPDATED = OTE_PREFIX + "!Dark_Purple!Your chat color has been updated";
STARTER_FAILURE_CONSOLE = OTE_PREFIX + "!Dark_Red!This command can only be executed from within the game";
STARTER_FAILURE_PERMISSIONS = OTE_PREFIX + "!Dark_Red!This command can only be executed by server operators";
STARTER_KIT_GIVEN = OTE_PREFIX + "!Dark_Purple!You have been given a starter kit. Welcome to the server.";
}
}

View file

@ -0,0 +1,38 @@
package dev.zontreck.otemod.implementation;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.LongTag;
import java.time.Instant;
public class PlayerFirstJoinTag
{
private PlayerFirstJoinTag(long time)
{
LastGiven = time;
}
public long LastGiven;
public static final String ID = "firstjoin";
public static final String LAST_GIVEN_TAG = "last";
public static PlayerFirstJoinTag now()
{
return new PlayerFirstJoinTag(Instant.now().getEpochSecond());
}
public void save(CompoundTag parent)
{
CompoundTag tag = new CompoundTag();
tag.put(LAST_GIVEN_TAG, LongTag.valueOf(LastGiven));
parent.put(ID, tag);
}
public static PlayerFirstJoinTag load(CompoundTag tag)
{
if(!tag.contains(ID)) return null;
CompoundTag me = tag.getCompound(ID);
return new PlayerFirstJoinTag(me.getLong(LAST_GIVEN_TAG));
}
}

View file

@ -0,0 +1,92 @@
package dev.zontreck.otemod.implementation.vault;
import dev.zontreck.libzontreck.profiles.Profile;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.LongTag;
import net.minecraft.nbt.NbtIo;
import net.minecraftforge.items.ItemStackHandler;
import java.io.File;
import java.io.IOException;
public class Starter {
public File file_location;
public final boolean isNew;
private long LastChanged;
private CompoundTag tag;
protected Starter(File loc)
{
file_location=loc;
isNew=!file_location.exists();
CompoundTag tag;
if(!isNew){
try {
tag = NbtIo.read(loc);
this.tag = tag.getCompound("inventory");
this.LastChanged = tag.getLong("changed");
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* This will return the contents of the NBT Vault
* @return
*/
public CompoundTag getContents(){
return tag;
}
/**
* This sets the internal compound tag of the vault provider, but also saves it to the file immediately.
* @param newTag
*/
public void setContents(CompoundTag newTag)
{
tag=newTag;
commit();
}
public void setLastChanged(long change)
{
LastChanged = change;
}
public long getLastChanged() {
return LastChanged;
}
private void commit()
{
CompoundTag newTag = new CompoundTag();
newTag.put("inventory", tag);
newTag.put("changed", LongTag.valueOf(LastChanged));
try {
NbtIo.write(newTag, file_location);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* This is called to dispose of the vault and the vault file
*/
public void delete()
{
if(file_location.exists())
file_location.delete();
}
public ItemStackHandler getItems()
{
ItemStackHandler H = new ItemStackHandler(32);
H.deserializeNBT(getContents());
return H;
}
}

View file

@ -0,0 +1,105 @@
package dev.zontreck.otemod.implementation.vault;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.profiles.Profile;
import dev.zontreck.libzontreck.profiles.UserProfileNotYetExistsException;
import dev.zontreck.libzontreck.util.ChatHelpers;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.configs.OTEServerConfig;
import dev.zontreck.otemod.implementation.events.VaultModifiedEvent;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
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.minecraft.world.item.Items;
import net.minecraftforge.items.ItemStackHandler;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class StarterContainer
{
public static Map<UUID, StarterContainer> VAULT_REGISTRY = new HashMap<>();
public StarterMenu theContainer;
public ItemStackHandler myInventory;
public ItemStackHandler startingInventory;
public MenuConstructor serverMenu;
public UUID owner;
private MinecraftServer server;
public final UUID VaultID;
public Starter main_accessor;
public StarterContainer(ServerPlayer player) throws NoMoreVaultException {
myInventory = new ItemStackHandler(54); // Vaults have a fixed size at the same as a double chest
startingInventory = new ItemStackHandler(64);
theContainer = new StarterMenu(player.containerCounter+1, player.getInventory(), myInventory, BlockPos.ZERO, player);
VaultID = theContainer.VaultMenuID;
owner = player.getUUID();
server=player.server;
serverMenu = theContainer.getServerMenu(myInventory);
// Check database for vault
StarterProvider.VaultAccessStrategy strategy;
strategy = StarterProvider.check();
if(strategy == StarterProvider.VaultAccessStrategy.CREATE || strategy == StarterProvider.VaultAccessStrategy.OPEN)
{
Starter accessor = StarterProvider.getStarter();
if(accessor.isNew)
{
main_accessor=accessor;
return;
}else {
myInventory.deserializeNBT(accessor.getContents());
startingInventory.deserializeNBT(accessor.getContents());
}
main_accessor=accessor;
}else {
// DENY
throw new NoMoreVaultException("No more vaults can be created", 0);
}
// Container is now ready to be sent to the client!
}
public void commit()
{
boolean isEmpty=true;
CompoundTag saved = myInventory.serializeNBT();
ChatHelpers.broadcastToAbove(owner, Component.literal(ChatColor.BOLD+ChatColor.DARK_GREEN+"Saving the starter kit's contents..."), server);
main_accessor.setLastChanged(Instant.now().getEpochSecond());
Profile profile=null;
try {
profile = Profile.get_profile_of(owner.toString());
} catch (UserProfileNotYetExistsException e) {
e.printStackTrace();
return;
}
for(int i = 0;i<myInventory.getSlots();i++)
{
ItemStack is = myInventory.getStackInSlot(i);
if(!is.is(Items.AIR))
{
isEmpty=false;
}
}
if(!isEmpty)
main_accessor.setContents(saved);
else
main_accessor.delete();
VaultModifiedEvent vme = new VaultModifiedEvent(-2, profile, VaultProvider.getInUse(profile), myInventory, startingInventory);
OTEMod.bus.post(vme);
}
}

View file

@ -0,0 +1,150 @@
package dev.zontreck.otemod.implementation.vault;
import dev.zontreck.otemod.implementation.inits.ModMenuTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
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;
import java.util.Map;
import java.util.UUID;
public class StarterMenu extends AbstractContainerMenu
{
//private final ContainerLevelAccess containerAccess;
public final UUID VaultMenuID;
public final Player thePlayer;
public StarterMenu(int id, Inventory player, FriendlyByteBuf buf)
{
this(id, player, new ItemStackHandler(54), player.player.getOnPos(), player.player);
}
public StarterMenu(int id, Inventory player, IItemHandler slots, BlockPos pos, Player play)
{
super(ModMenuTypes.VAULT.get(), id);
thePlayer=play;
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 StarterMenu(id, player, inventory, BlockPos.ZERO, player.player);
}
public void doCommitAction()
{
// Locate the Vault in the Vault Registry and commit changes.
// Search for myself!
for(Map.Entry<UUID,StarterContainer> e : StarterContainer.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,56 @@
package dev.zontreck.otemod.implementation.vault;
import dev.zontreck.libzontreck.profiles.Profile;
import dev.zontreck.otemod.configs.OTEServerConfig;
import dev.zontreck.otemod.database.OTEDatastore;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public class StarterProvider extends OTEDatastore
{
public enum VaultAccessStrategy
{
OPEN,
CREATE,
DENY
}
public static final Path FILE_TREE_PATH = of("starter");
public static VaultAccessStrategy check()
{
if(!FILE_TREE_PATH.toFile().exists())
{
try {
Files.createDirectory(FILE_TREE_PATH);
} catch (IOException e) {
e.printStackTrace();
}
}
Path starterVaultPath = FILE_TREE_PATH.resolve("starter.nbt");
File vaultFile = starterVaultPath.toFile();
if(vaultFile.exists())
{
return VaultAccessStrategy.OPEN;
}else {
return VaultAccessStrategy.CREATE;
}
}
public static Starter getStarter() throws NoMoreVaultException
{
VaultAccessStrategy strat = check();
if(strat == VaultAccessStrategy.CREATE || strat == VaultAccessStrategy.OPEN)
{
Path userVault = FILE_TREE_PATH.resolve("starter.nbt");
Starter v = new Starter(userVault.toFile());
return v;
}else return null;
}
}

View file

@ -0,0 +1,70 @@
package dev.zontreck.otemod.implementation.vault;
import com.mojang.blaze3d.systems.RenderSystem;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.networking.ModMessages;
import dev.zontreck.otemod.networking.packets.OpenStarterVaultC2SPacket;
import dev.zontreck.otemod.networking.packets.OpenVaultC2SPacket;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
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;
import net.minecraft.world.entity.player.Player;
import java.util.UUID;
public class StarterScreen extends AbstractContainerScreen <StarterMenu>
{
// 176x224
public final UUID VaultMenuID;
public final Player thePlayer;
public final StarterMenu THE_CONTAINER;
private static final ResourceLocation TEXTURE = new ResourceLocation(OTEMod.MOD_ID, "textures/gui/vault.png");
public StarterScreen(StarterMenu container, Inventory playerInv, Component comp){
super(container, playerInv, comp);
thePlayer=playerInv.player;
this.VaultMenuID = container.VaultMenuID;
this.leftPos = 0;
this.topPos = 0;
this.THE_CONTAINER=container;
this.imageWidth = 207;
this.imageHeight = 238;
}
@Override
public void render(GuiGraphics stack, int mouseX, int mouseY, float partialTicks)
{
this.renderBackground(stack);
super.render(stack, mouseX, mouseY, partialTicks);
this.renderTooltip(stack, mouseX, mouseY);
}
@Override
protected void init()
{
super.init();
this.inventoryLabelY = 12;
this.inventoryLabelX = 63;
}
@Override
protected void renderBg(GuiGraphics pGuiGraphics, float pPartialTick, int pMouseX, int pMouseY)
{
renderBackground(pGuiGraphics);
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor (1.0f, 1.0f, 1.0f, 1.0f);
RenderSystem.setShaderTexture(0, TEXTURE);
pGuiGraphics.blit(TEXTURE, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight);
}
}

View file

@ -29,6 +29,15 @@ public class VaultWatcher {
entry.getValue().commit();
}
}
} else if(ev.getContainer() instanceof StarterMenu)
{
for(Map.Entry<UUID, StarterContainer> entry : StarterContainer.VAULT_REGISTRY.entrySet())
{
if(entry.getKey() == ev.getEntity().getUUID())
{
entry.getValue().commit();
}
}
}
}
}

View file

@ -43,7 +43,9 @@ public class PossBallItem extends Item
if(!pLevel.isClientSide)
{
ThrownPossBall TPB = new ThrownPossBall(pLevel, pPlayer);
TPB.setItem(stack);
if(pPlayer.getAbilities().instabuild) TPB.setItem(stack.copy());
else
TPB.setItem(stack);
TPB.shootFromRotation(pPlayer, pPlayer.getXRot(), pPlayer.getYRot(), 0.0F, 1.5F, 1.0F);
pLevel.addFreshEntity(TPB);
}
@ -52,6 +54,8 @@ public class PossBallItem extends Item
if(!pPlayer.getAbilities().instabuild)
{
stack.shrink(1);
}else {
}
return super.use(pLevel, pPlayer, pUsedHand);
}

View file

@ -0,0 +1,41 @@
package dev.zontreck.otemod.networking.packets;
import dev.zontreck.otemod.commands.vaults.StarterCommand;
import dev.zontreck.otemod.commands.vaults.VaultCommand;
import dev.zontreck.otemod.implementation.vault.StarterContainer;
import dev.zontreck.otemod.implementation.vault.VaultContainer;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkEvent;
import java.util.function.Supplier;
// This packet is only ever sent from the client to the server when requesting to open vaults using the EaseOfUse Buttons
public class OpenStarterVaultC2SPacket {
public OpenStarterVaultC2SPacket()
{
}
public OpenStarterVaultC2SPacket(FriendlyByteBuf buf)
{
}
public void toBytes(FriendlyByteBuf buf)
{
}
public boolean handle(Supplier<NetworkEvent.Context> supplier)
{
NetworkEvent.Context ctx = supplier.get();
ctx.enqueueWork(()->{
// On server now
ServerPlayer player = ctx.getSender();
StarterCommand.doOpen(player);
});
return true;
}
}