Skip to content

Commit

Permalink
Added Travelers Backpack and Home Command
Browse files Browse the repository at this point in the history
  • Loading branch information
Petersil1998 committed Dec 8, 2020
1 parent 8a00e5b commit f358ebb
Show file tree
Hide file tree
Showing 28 changed files with 612 additions and 27 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# This is required to provide enough memory for the Minecraft decompilation process.
org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false
mod_version=1.0.7
mod_version=1.0.8
13 changes: 11 additions & 2 deletions src/main/java/net/petersil98/utilcraft/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@
import net.petersil98.utilcraft.container.DisenchantmentTableContainer;
import net.petersil98.utilcraft.container.ModContainer;
import net.petersil98.utilcraft.container.SecureChestContainer;
import net.petersil98.utilcraft.container.TravelersBackpackContainer;
import net.petersil98.utilcraft.data.KeyBindings;
import net.petersil98.utilcraft.data.capabilities.home.CapabilityHome;
import net.petersil98.utilcraft.data.capabilities.inventory.CapabilityInventory;
import net.petersil98.utilcraft.data.capabilities.vein_miner.CapabilityVeinMiner;
import net.petersil98.utilcraft.network.PacketHandler;
import net.petersil98.utilcraft.renderer.SecureChestItemTileEntityRenderer;
import net.petersil98.utilcraft.renderer.SecureChestTileEntityRenderer;
import net.petersil98.utilcraft.screen.DisenchantmentTableScreen;
import net.petersil98.utilcraft.screen.SecureChestScreen;
import net.petersil98.utilcraft.screen.TravelersBackpackScreen;
import net.petersil98.utilcraft.tile_entities.DisenchantmentTableTileEntity;
import net.petersil98.utilcraft.tile_entities.ModSignTileEntity;
import net.petersil98.utilcraft.tile_entities.ModTileEntities;
Expand Down Expand Up @@ -77,8 +82,9 @@ public Main() {
}

private void setup(final FMLCommonSetupEvent event) {

//CapabilityTileEntityOwner.register();
CapabilityInventory.register();
CapabilityVeinMiner.register();
CapabilityHome.register();
PacketHandler.registerMessages();
}

Expand All @@ -88,6 +94,7 @@ private void clientSetup(final FMLClientSetupEvent event){
RenderTypeLookup.setRenderLayer(ModBlocks.SAKURA_DOOR, RenderType.getCutout());
ScreenManager.registerFactory(ModContainer.DISENCHANTMENT_BLOCK_CONTAINER, DisenchantmentTableScreen::new);
ScreenManager.registerFactory(ModContainer.SECURE_CHEST_CONTAINER, SecureChestScreen::new);
ScreenManager.registerFactory(ModContainer.TRAVELERS_BACKPACK_CONTAINER, TravelersBackpackScreen::new);
ClientRegistry.bindTileEntityRenderer(ModTileEntities.MOD_SIGN, SignTileEntityRenderer::new);
ClientRegistry.bindTileEntityRenderer(ModTileEntities.SECURE_CHEST, SecureChestTileEntityRenderer::new);
ClientRegistry.registerKeyBinding(KeyBindings.VEIN_MINER);
Expand Down Expand Up @@ -197,6 +204,7 @@ public static void registerItems(final RegistryEvent.Register<Item> itemRegistry
itemRegistryEvent.getRegistry().register(new RoseQuartzSuperHammer().setRegistryName("rose_quartz_super_hammer"));
itemRegistryEvent.getRegistry().register(new RoseQuartzSuperShovel().setRegistryName("rose_quartz_super_shovel"));
itemRegistryEvent.getRegistry().register(new SakuraSignItem().setRegistryName("sakura_sign"));
itemRegistryEvent.getRegistry().register(new TravelersBackpack().setRegistryName("travelers_backpack"));
}

@SubscribeEvent
Expand Down Expand Up @@ -225,6 +233,7 @@ public static void registerTileEntities(final RegistryEvent.Register<TileEntityT
public static void registerContainer(final RegistryEvent.Register<ContainerType<?>> containerRegister) {
containerRegister.getRegistry().register(new ContainerType<>(DisenchantmentTableContainer::new).setRegistryName("disenchantment_table"));
containerRegister.getRegistry().register(new ContainerType<>(SecureChestContainer::new).setRegistryName("secure_chest"));
containerRegister.getRegistry().register(new ContainerType<>(TravelersBackpackContainer::new).setRegistryName("travelers_backpack"));
}
}
@Mod.EventBusSubscriber(bus= Mod.EventBusSubscriber.Bus.FORGE)
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/net/petersil98/utilcraft/commands/HomeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package net.petersil98.utilcraft.commands;

import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.play.server.SPlayerPositionLookPacket;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.server.TicketType;
import net.petersil98.utilcraft.data.capabilities.home.CapabilityHome;
import org.apache.logging.log4j.LogManager;

import java.util.EnumSet;
import java.util.Set;

public class HomeCommand {

public static ArgumentBuilder<CommandSource, ?> register(){
return Commands.literal("home")
.executes(context -> {
teleportHome(context.getSource());
return 1;
})
.then(Commands.literal("set").executes(context -> {
setHome(context.getSource());
return 1;
}));
}

private static void setHome(CommandSource source) throws CommandSyntaxException {
ServerPlayerEntity player = source.asPlayer();
BlockPos position = player.getPosition();
player.getCapability(CapabilityHome.HOME_CAPABILITY).ifPresent(iHome -> {
iHome.setHome(position);
source.sendFeedback(new TranslationTextComponent("home.utilcraft.set_home"), true);
});
}

private static void teleportHome(CommandSource source) throws CommandSyntaxException {
ServerPlayerEntity player = source.asPlayer();
player.getCapability(CapabilityHome.HOME_CAPABILITY).ifPresent(iHome -> {
BlockPos home = iHome.getHome();
if(home != null && !home.equals(BlockPos.ZERO)) {
teleportToPos(player, player.getServerWorld(), home);
source.sendFeedback(new TranslationTextComponent("home.utilcraft.teleported"), true);
} else {
source.sendFeedback(new TranslationTextComponent("home.utilcraft.not_set"), true);
}
});
}

private static void teleportToPos(ServerPlayerEntity target, ServerWorld worldIn, BlockPos position) {
Set<SPlayerPositionLookPacket.Flags> set = EnumSet.noneOf(SPlayerPositionLookPacket.Flags.class);

set.add(SPlayerPositionLookPacket.Flags.X_ROT);
set.add(SPlayerPositionLookPacket.Flags.Y_ROT);

ChunkPos chunkpos = new ChunkPos(position);
worldIn.getChunkProvider().registerTicket(TicketType.POST_TELEPORT, chunkpos, 1, target.getEntityId());
target.stopRiding();
if (target.isSleeping()) {
target.stopSleepInBed(true, true);
}

if (worldIn == target.world) {
target.connection.setPlayerLocation(position.getX(), position.getY(), position.getZ(), 0, 0, set);
} else {
target.teleport(worldIn, position.getX(), position.getY(), position.getZ(), 0, 0);
}

target.setRotationYawHead(0);

if (!target.isElytraFlying()) {
target.setMotion(target.getMotion().mul(1.0D, 0.0D, 1.0D));
target.setOnGround(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ModCommands {
public static void register(CommandDispatcher<CommandSource> dispatcher){
LiteralCommandNode<CommandSource> command = dispatcher.register(
Commands.literal("utilcraft").then(TrustedPlayersCommand.register())
.then(HomeCommand.register())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ public class ModContainer {

@ObjectHolder("utilcraft:secure_chest")
public static ContainerType<SecureChestContainer> SECURE_CHEST_CONTAINER;

@ObjectHolder("utilcraft:travelers_backpack")
public static ContainerType<TravelersBackpackContainer> TRAVELERS_BACKPACK_CONTAINER;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
import net.minecraft.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.items.SlotItemHandler;

import javax.annotation.Nonnull;

public class SecureChestContainer extends Container {

private final ItemStackHandler inventory;
private final IItemHandler inventory;
private final int numRows;

public SecureChestContainer(int id, PlayerInventory playerInventory) {
this(id, playerInventory, new ItemStackHandler(27));
}

public SecureChestContainer(int id, PlayerInventory playerInventory, ItemStackHandler inventory) {
public SecureChestContainer(int id, PlayerInventory playerInventory, IItemHandler inventory) {
super(ModContainer.SECURE_CHEST_CONTAINER, id);
this.inventory = inventory;
this.numRows = 3;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package net.petersil98.utilcraft.container;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.items.SlotItemHandler;

import javax.annotation.Nonnull;

public class TravelersBackpackContainer extends Container {

private final IItemHandler inventory;
private final int numRows;

public TravelersBackpackContainer(int id, PlayerInventory playerInventory) {
this(id, playerInventory, new ItemStackHandler(27));
}

public TravelersBackpackContainer(int id, PlayerInventory playerInventory, IItemHandler inventory) {
super(ModContainer.TRAVELERS_BACKPACK_CONTAINER, id);
this.inventory = inventory;
this.numRows = inventory.getSlots()/9;
addSlots(playerInventory);
}

protected void addSlots(PlayerInventory playerInventory)
{
int i = (this.numRows - 4) * 18;

for(int j = 0; j < this.numRows; ++j) {
for(int k = 0; k < 9; ++k) {
this.addSlot(new SlotItemHandler(inventory, k + j * 9, 8 + k * 18, 18 + j * 18));
}
}
for(int l = 0; l < 3; ++l) {
for(int j1 = 0; j1 < 9; ++j1) {
this.addSlot(new Slot(playerInventory, j1 + l * 9 + 9, 8 + j1 * 18, 103 + l * 18 + i));
}
}

for(int i1 = 0; i1 < 9; ++i1) {
this.addSlot(new Slot(playerInventory, i1, 8 + i1 * 18, 161 + i));
}
}

/**
* Determines whether supplied player can use this container
*/
public boolean canInteractWith(@Nonnull PlayerEntity playerIn) {
return true;
}

/**
* Handle when the stack in slot {@code index} is shift-clicked. Normally this moves the stack between the player
* inventory and the other inventory(s).
*/
@Nonnull
public ItemStack transferStackInSlot(@Nonnull PlayerEntity playerIn, int index) {
ItemStack itemstack = ItemStack.EMPTY;
Slot slot = this.inventorySlots.get(index);
if (slot != null && slot.getHasStack()) {
ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy();
if (index < this.numRows * 9) {
if (!this.mergeItemStack(itemstack1, this.numRows * 9, this.inventorySlots.size(), true)) {
return ItemStack.EMPTY;
}
} else if (!this.mergeItemStack(itemstack1, 0, this.numRows * 9, false)) {
return ItemStack.EMPTY;
}

if (itemstack1.isEmpty()) {
slot.putStack(ItemStack.EMPTY);
} else {
slot.onSlotChanged();
}
}

return itemstack;
}

/**
* Called when the container is closed.
*/
public void onContainerClosed(@Nonnull PlayerEntity playerIn) {
super.onContainerClosed(playerIn);
}

@OnlyIn(Dist.CLIENT)
public int getNumRows() {
return this.numRows;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.petersil98.utilcraft.data.capabilities.home;

import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.CapabilityManager;

import javax.annotation.Nullable;

public class CapabilityHome {
@CapabilityInject(IHome.class)
public static Capability<IHome> HOME_CAPABILITY = null;

public static void register() {
CapabilityManager.INSTANCE.register(IHome.class, new Storage(), DefaultHome::new);
}

public static class Storage implements Capability.IStorage<IHome> {

@Nullable
@Override
public INBT writeNBT(Capability<IHome> capability, IHome instance, Direction side) {
CompoundNBT tag = new CompoundNBT();
BlockPos home = instance.getHome();
if(home != null) {
int[] cords = {home.getX(), home.getY(), home.getZ()};
tag.putIntArray("home", cords);
}
return tag;
}

@Override
public void readNBT(Capability<IHome> capability, IHome instance, Direction side, INBT nbt) {
CompoundNBT tag = ((CompoundNBT)nbt);
int[] cords = tag.getIntArray("home");
BlockPos home = BlockPos.ZERO;
if(cords.length == 3) {
home = new BlockPos(cords[0], cords[1], cords[2]);
}
instance.setHome(home);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.petersil98.utilcraft.data.capabilities.home;

import net.minecraft.util.math.BlockPos;

public class DefaultHome implements IHome {

private BlockPos home;

@Override
public void setHome(BlockPos home) {
this.home = home;
}

public BlockPos getHome() {
return home;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.petersil98.utilcraft.data.capabilities.home;

import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class HomeProvider implements ICapabilitySerializable<CompoundNBT> {

private final DefaultHome home = new DefaultHome();
private final LazyOptional<IHome> homeOptional = LazyOptional.of(() -> home);

public void invalidate() {
homeOptional.invalidate();
}

@Nonnull
@Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
if(cap == CapabilityHome.HOME_CAPABILITY) {
return homeOptional.cast();
} else {
return LazyOptional.empty();
}
}

@Override
public CompoundNBT serializeNBT() {
if (CapabilityHome.HOME_CAPABILITY == null) {
return new CompoundNBT();
} else {
return (CompoundNBT) CapabilityHome.HOME_CAPABILITY.writeNBT(home, null);
}
}

@Override
public void deserializeNBT(CompoundNBT nbt) {
if (CapabilityHome.HOME_CAPABILITY != null) {
CapabilityHome.HOME_CAPABILITY.readNBT(home, null, nbt);
}
}
}
Loading

0 comments on commit f358ebb

Please sign in to comment.