-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Travelers Backpack and Home Command
- Loading branch information
1 parent
8a00e5b
commit f358ebb
Showing
28 changed files
with
612 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/net/petersil98/utilcraft/commands/HomeCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/main/java/net/petersil98/utilcraft/container/TravelersBackpackContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/net/petersil98/utilcraft/data/capabilities/home/CapabilityHome.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/net/petersil98/utilcraft/data/capabilities/home/DefaultHome.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/net/petersil98/utilcraft/data/capabilities/home/HomeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.