Skip to content

Commit

Permalink
add expanded remote to curios keybind with some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Lothrazar committed Dec 5, 2024
1 parent a59f736 commit fbbc86d
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

import java.util.List;
import com.lothrazar.library.item.ItemFlib;
import com.lothrazar.storagenetwork.StorageNetworkMod;
import com.lothrazar.storagenetwork.api.DimPos;
import com.lothrazar.storagenetwork.api.EnumSortType;
import com.lothrazar.storagenetwork.block.main.TileMain;
import com.lothrazar.storagenetwork.block.request.TileRequest;
import com.lothrazar.storagenetwork.registry.ConfigRegistry;
import com.lothrazar.storagenetwork.registry.SsnRegistry;
import com.lothrazar.storagenetwork.util.UtilRemote;
import com.lothrazar.storagenetwork.util.UtilTileEntity;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
Expand All @@ -27,19 +24,17 @@
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.network.NetworkHooks;

public class ItemStorageCraftingRemote extends ItemFlib implements MenuProvider {
public class ItemRemote extends ItemFlib implements MenuProvider {

public static final String NBT_JEI = TileRequest.NBT_JEI;
public static final String NBT_BOUND = "bound";
public static final String NBT_SORT = "sort";
public static final String NBT_DOWN = "down";

public ItemStorageCraftingRemote(Properties properties) {
public ItemRemote(Properties properties) {
super(properties.stacksTo(1));
}

Expand Down Expand Up @@ -107,62 +102,10 @@ public void appendHoverText(ItemStack stack, Level worldIn, List<Component> tool
}
}

@SuppressWarnings("deprecation")
public static boolean openRemote(Level world, Player player, ItemStack itemStackIn, ItemStorageCraftingRemote thiss) {
DimPos dp = DimPos.getPosStored(itemStackIn);
if (dp == null) {
//unbound or invalid data
UtilTileEntity.statusMessage(player, "item.remote.notconnected");
return false;
}
//assume we are in the same world
BlockPos posTarget = dp.getBlockPos();
if (ConfigRegistry.ITEMRANGE.get() != -1) {
double distance = player.distanceToSqr(posTarget.getX() + 0.5D, posTarget.getY() + 0.5D, posTarget.getZ() + 0.5D);
if (distance >= ConfigRegistry.ITEMRANGE.get()) {
UtilTileEntity.statusMessage(player, "item.remote.outofrange");
return false;
}
}
//else it is -1 so dont even check distance
//k now server only
if (world.isClientSide) {
return false;
}
//now check the dimension world
ServerLevel serverTargetWorld = null;
try {
serverTargetWorld = DimPos.stringDimensionLookup(dp.getDimension(), world.getServer());
if (serverTargetWorld == null) {
StorageNetworkMod.LOGGER.error("Missing dimension key " + dp.getDimension());
}
}
catch (Exception e) {
//
StorageNetworkMod.LOGGER.error("unknown exception on dim " + dp.getDimension(), e);
return false;
}
//now check is the area chunk loaded
if (!serverTargetWorld.isAreaLoaded(posTarget, 1)) {
UtilTileEntity.chatMessage(player, "item.remote.notloaded");
StorageNetworkMod.LOGGER.info(UtilTileEntity.lang("item.remote.notloaded") + posTarget);
return false;
}
BlockEntity tile = serverTargetWorld.getBlockEntity(posTarget);
if (tile instanceof TileMain) {
NetworkHooks.openScreen((ServerPlayer) player, thiss);
return true;
}
else {
player.displayClientMessage(Component.translatable("item.remote.notfound"), true);
return false;
}
}

@Override
public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
ItemStack itemStackIn = player.getItemInHand(hand);
if (openRemote(world, player, itemStackIn, this)) {
if (UtilRemote.openRemote(world, player, itemStackIn, this)) {
// ok great
return InteractionResultHolder.success(itemStackIn);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,42 +58,42 @@ protected void renderBg(GuiGraphics ms, float partialTicks, int mouseX, int mous

@Override
public boolean getDownwards() {
return ItemStorageCraftingRemote.getDownwards(remote);
return ItemRemote.getDownwards(remote);
}

@Override
public void setDownwards(boolean val) {
ItemStorageCraftingRemote.setDownwards(remote, val);
ItemRemote.setDownwards(remote, val);
}

@Override
public EnumSortType getSort() {
return ItemStorageCraftingRemote.getSort(remote);
return ItemRemote.getSort(remote);
}

@Override
public void setSort(EnumSortType val) {
ItemStorageCraftingRemote.setSort(remote, val);
ItemRemote.setSort(remote, val);
}

@Override
public boolean isJeiSearchSynced() {
return ItemStorageCraftingRemote.isJeiSearchSynced(remote);
return ItemRemote.isJeiSearchSynced(remote);
}

@Override
public void setJeiSearchSynced(boolean val) {
ItemStorageCraftingRemote.setJeiSearchSynced(remote, val);
ItemRemote.setJeiSearchSynced(remote, val);
}

@Override
public boolean getAutoFocus() {
return ItemStorageCraftingRemote.getAutoFocus(remote);
return ItemRemote.getAutoFocus(remote);
}

@Override
public void setAutoFocus(boolean b) {
ItemStorageCraftingRemote.setAutoFocus(remote, b);
ItemRemote.setAutoFocus(remote, b);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,42 +56,42 @@ protected void renderBg(GuiGraphics ms, float partialTicks, int mouseX, int mous

@Override
public boolean getDownwards() {
return ItemStorageCraftingRemote.getDownwards(remote);
return ItemRemote.getDownwards(remote);
}

@Override
public void setDownwards(boolean val) {
ItemStorageCraftingRemote.setDownwards(remote, val);
ItemRemote.setDownwards(remote, val);
}

@Override
public EnumSortType getSort() {
return ItemStorageCraftingRemote.getSort(remote);
return ItemRemote.getSort(remote);
}

@Override
public void setSort(EnumSortType val) {
ItemStorageCraftingRemote.setSort(remote, val);
ItemRemote.setSort(remote, val);
}

@Override
public boolean isJeiSearchSynced() {
return ItemStorageCraftingRemote.isJeiSearchSynced(remote);
return ItemRemote.isJeiSearchSynced(remote);
}

@Override
public void setJeiSearchSynced(boolean val) {
ItemStorageCraftingRemote.setJeiSearchSynced(remote, val);
ItemRemote.setJeiSearchSynced(remote, val);
}

@Override
public boolean getAutoFocus() {
return ItemStorageCraftingRemote.getAutoFocus(remote);
return ItemRemote.getAutoFocus(remote);
}

@Override
public void setAutoFocus(boolean b) {
ItemStorageCraftingRemote.setAutoFocus(remote, b);
ItemRemote.setAutoFocus(remote, b);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,42 +56,42 @@ protected void renderBg(GuiGraphics ms, float partialTicks, int mouseX, int mous

@Override
public boolean getDownwards() {
return ItemStorageCraftingRemote.getDownwards(remote);
return ItemRemote.getDownwards(remote);
}

@Override
public void setDownwards(boolean val) {
ItemStorageCraftingRemote.setDownwards(remote, val);
ItemRemote.setDownwards(remote, val);
}

@Override
public EnumSortType getSort() {
return ItemStorageCraftingRemote.getSort(remote);
return ItemRemote.getSort(remote);
}

@Override
public void setSort(EnumSortType val) {
ItemStorageCraftingRemote.setSort(remote, val);
ItemRemote.setSort(remote, val);
}

@Override
public boolean isJeiSearchSynced() {
return ItemStorageCraftingRemote.isJeiSearchSynced(remote);
return ItemRemote.isJeiSearchSynced(remote);
}

@Override
public void setJeiSearchSynced(boolean val) {
ItemStorageCraftingRemote.setJeiSearchSynced(remote, val);
ItemRemote.setJeiSearchSynced(remote, val);
}

@Override
public boolean getAutoFocus() {
return ItemStorageCraftingRemote.getAutoFocus(remote);
return ItemRemote.getAutoFocus(remote);
}

@Override
public void setAutoFocus(boolean b) {
ItemStorageCraftingRemote.setAutoFocus(remote, b);
ItemRemote.setAutoFocus(remote, b);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package com.lothrazar.storagenetwork.network;

import java.util.function.Supplier;
import org.apache.commons.lang3.tuple.Triple;
import com.lothrazar.storagenetwork.item.remote.ItemStorageCraftingRemote;
import com.lothrazar.storagenetwork.registry.SsnRegistry;
import com.lothrazar.storagenetwork.util.UtilInventory;
import com.lothrazar.storagenetwork.util.UtilRemote;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.network.NetworkEvent;

public class KeybindCurioMessage {
Expand All @@ -19,18 +15,7 @@ public static void handle(KeybindCurioMessage message, Supplier<NetworkEvent.Con
ctx.get().enqueueWork(() -> {
ServerPlayer player = ctx.get().getSender();
ServerLevel serverWorld = (ServerLevel) player.level();
Triple<String, Integer, ItemStack> searchCrafting = UtilInventory.getCurioRemote(player, SsnRegistry.Items.CRAFTING_REMOTE.get());
ItemStack craftingRemote = searchCrafting.getRight();
if (!craftingRemote.isEmpty()) {
ItemStorageCraftingRemote.openRemote(serverWorld, player, craftingRemote, SsnRegistry.Items.CRAFTING_REMOTE.get());
}
else { //crafting is the upgrade, so otherwise do regular
Triple<String, Integer, ItemStack> searchResult = UtilInventory.getCurioRemote(player, SsnRegistry.Items.INVENTORY_REMOTE.get());
ItemStack curioRemote = searchResult.getRight();
if (!curioRemote.isEmpty()) {
ItemStorageCraftingRemote.openRemote(serverWorld, player, curioRemote, SsnRegistry.Items.INVENTORY_REMOTE.get());
}
}
UtilRemote.searchAndOpen(player, serverWorld);
});
ctx.get().setPacketHandled(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.lothrazar.storagenetwork.api.ITileNetworkSync;
import com.lothrazar.storagenetwork.item.remote.ContainerNetworkCraftingRemote;
import com.lothrazar.storagenetwork.item.remote.ContainerNetworkRemote;
import com.lothrazar.storagenetwork.item.remote.ItemStorageCraftingRemote;
import com.lothrazar.storagenetwork.item.remote.ItemRemote;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
Expand Down Expand Up @@ -49,20 +49,20 @@ public static void handle(SettingsSyncMessage message, Supplier<NetworkEvent.Con
}
else if (player.containerMenu instanceof ContainerNetworkCraftingRemote remoteContainer) {
ItemStack stackPlayerHeld = remoteContainer.getRemote();
if (stackPlayerHeld.getItem() instanceof ItemStorageCraftingRemote) {
ItemStorageCraftingRemote.setSort(stackPlayerHeld, message.sort);
ItemStorageCraftingRemote.setDownwards(stackPlayerHeld, message.direction);
ItemStorageCraftingRemote.setJeiSearchSynced(stackPlayerHeld, message.jeiSync);
ItemStorageCraftingRemote.setAutoFocus(stackPlayerHeld, message.autoFocus);
if (stackPlayerHeld.getItem() instanceof ItemRemote) {
ItemRemote.setSort(stackPlayerHeld, message.sort);
ItemRemote.setDownwards(stackPlayerHeld, message.direction);
ItemRemote.setJeiSearchSynced(stackPlayerHeld, message.jeiSync);
ItemRemote.setAutoFocus(stackPlayerHeld, message.autoFocus);
}
}
else if (player.containerMenu instanceof ContainerNetworkRemote rcc) {
ItemStack stackPlayerHeld = rcc.getRemote();
if (stackPlayerHeld.getItem() instanceof ItemStorageCraftingRemote) {
ItemStorageCraftingRemote.setSort(stackPlayerHeld, message.sort);
ItemStorageCraftingRemote.setDownwards(stackPlayerHeld, message.direction);
ItemStorageCraftingRemote.setJeiSearchSynced(stackPlayerHeld, message.jeiSync);
ItemStorageCraftingRemote.setAutoFocus(stackPlayerHeld, message.autoFocus);
if (stackPlayerHeld.getItem() instanceof ItemRemote) {
ItemRemote.setSort(stackPlayerHeld, message.sort);
ItemRemote.setDownwards(stackPlayerHeld, message.direction);
ItemRemote.setJeiSearchSynced(stackPlayerHeld, message.jeiSync);
ItemRemote.setAutoFocus(stackPlayerHeld, message.autoFocus);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import com.lothrazar.storagenetwork.item.remote.ContainerNetworkCraftingRemote;
import com.lothrazar.storagenetwork.item.remote.ContainerNetworkExpandedRemote;
import com.lothrazar.storagenetwork.item.remote.ContainerNetworkRemote;
import com.lothrazar.storagenetwork.item.remote.ItemStorageCraftingRemote;
import com.lothrazar.storagenetwork.item.remote.ItemRemote;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -100,12 +100,12 @@ public static class Items {
public static final RegistryObject<ItemUpgrade> STOCK_UPGRADE = ITEMS.register("stock_upgrade", () -> new ItemUpgrade(new Item.Properties()));
public static final RegistryObject<ItemUpgrade> OP_U = ITEMS.register("operation_upgrade", () -> new ItemUpgrade(new Item.Properties()));
public static final RegistryObject<ItemUpgrade> SINGLE_UPGRADE = ITEMS.register("single_upgrade", () -> new ItemUpgrade(new Item.Properties()));
public static final RegistryObject<ItemStorageCraftingRemote> INVENTORY_REMOTE = ITEMS.register("inventory_remote", () -> new ItemStorageCraftingRemote(new Item.Properties()));
public static final RegistryObject<ItemStorageCraftingRemote> CRAFTING_REMOTE = ITEMS.register("crafting_remote", () -> new ItemStorageCraftingRemote(new Item.Properties()));
public static final RegistryObject<ItemRemote> INVENTORY_REMOTE = ITEMS.register("inventory_remote", () -> new ItemRemote(new Item.Properties()));
public static final RegistryObject<ItemRemote> CRAFTING_REMOTE = ITEMS.register("crafting_remote", () -> new ItemRemote(new Item.Properties()));
public static final RegistryObject<Item> PICKER_REMOTE = ITEMS.register("picker_remote", () -> new ItemPicker(new Item.Properties()));
public static final RegistryObject<ItemCollector> COLLECTOR_REMOTE = ITEMS.register("collector_remote", () -> new ItemCollector(new Item.Properties()));
public static final RegistryObject<Item> BUILDER_REMOTE = ITEMS.register("builder_remote", () -> new ItemBuilder(new Item.Properties()));
public static final RegistryObject<Item> EXPANDED_REMOTE = ITEMS.register("expanded_remote", () -> new ItemStorageCraftingRemote(new Item.Properties()));
public static final RegistryObject<ItemRemote> EXPANDED_REMOTE = ITEMS.register("expanded_remote", () -> new ItemRemote(new Item.Properties()));
}

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
Expand Down
Loading

0 comments on commit fbbc86d

Please sign in to comment.