✨ Powered trading station
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
// 1.19.2 2023-08-15T19:12:23.552338231 Registrate Provider for trading_station [Recipes, Advancements, Loot tables, Tags (blocks), Tags (items), Tags (fluids), Tags (entity_types), Blockstates, Item models, Lang (en_us/en_ud)]
|
||||
// 1.19.2 2023-08-16T15:56:06.979095197 Registrate Provider for trading_station [Recipes, Advancements, Loot tables, Tags (blocks), Tags (items), Tags (fluids), Tags (entity_types), Blockstates, Item models, Lang (en_us/en_ud)]
|
||||
ac427e21e7a0915809800ce83800780c045678c6 assets/trading_station/blockstates/powered_trading_station.json
|
||||
391b0f87f6c9bd80e20dfc8b949e7dd99cd16fe9 assets/trading_station/blockstates/trading_station.json
|
||||
59d487ef1d18c0234dbb7c4b7a99eb0a8bf30520 assets/trading_station/lang/en_ud.json
|
||||
fcd793fe1e0ddcc1d5cc535bda29fe37fee4c1a2 assets/trading_station/lang/en_us.json
|
||||
2b8f51b49619e47225d4710f7917205a31ff362e assets/trading_station/lang/en_ud.json
|
||||
53756e3e6b11fb1c70ccf8979a5a36504a702fca assets/trading_station/lang/en_us.json
|
||||
373be6ce42eee460888161c967f098e5943e3778 assets/trading_station/models/item/powered_trading_station.json
|
||||
7ddda578b6d6072db924599a9959579470354897 assets/trading_station/models/item/trading_station.json
|
||||
c3b4c04c1cecdc5001fcddda7b30b11b15f0b2b2 data/trading_station/loot_tables/blocks/powered_trading_station.json
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
"block.trading_station.trading_station": "uoıʇɐʇS buıpɐɹ⟘",
|
||||
"config.jade.plugin_trading_station.trading_station_data": "ɐʇɐp uoıʇɐʇS buıpɐɹ⟘",
|
||||
"itemGroup.trading_station:main": "uoıʇɐʇS buıpɐɹ⟘",
|
||||
"trading_station.block.display": "uoıʇɐʇS buıpɐɹ⟘",
|
||||
"trading_station.recipe": "ǝdıɔǝɹ buıpɐɹ⟘",
|
||||
"trading_station.powered_trading_station.block.display": "uoıʇɐʇS buıpɐɹ⟘ pǝɹǝʍoԀ",
|
||||
"trading_station.select_target.button": "ʇǝbɹɐʇ ʇɔǝןǝS",
|
||||
"trading_station.select_target.clear": "ɹɐǝןƆ",
|
||||
"trading_station.select_target.title": "ʇǝbɹɐʇ ʇndʇno uɐ ʇɔǝןǝS",
|
||||
"trading_station.tooltip.progress": "%d%% :ssǝɹboɹԀ"
|
||||
"trading_station.tooltip.progress": "%d%% :ssǝɹboɹԀ",
|
||||
"trading_station.trading.recipe": "ǝdıɔǝɹ buıpɐɹ⟘",
|
||||
"trading_station.trading_station.block.display": "uoıʇɐʇS buıpɐɹ⟘"
|
||||
}
|
||||
@@ -3,10 +3,11 @@
|
||||
"block.trading_station.trading_station": "Trading Station",
|
||||
"config.jade.plugin_trading_station.trading_station_data": "Trading Station data",
|
||||
"itemGroup.trading_station:main": "Trading Station",
|
||||
"trading_station.block.display": "Trading Station",
|
||||
"trading_station.recipe": "Trading recipe",
|
||||
"trading_station.powered_trading_station.block.display": "Powered Trading Station",
|
||||
"trading_station.select_target.button": "Select target",
|
||||
"trading_station.select_target.clear": "Clear",
|
||||
"trading_station.select_target.title": "Select an output target",
|
||||
"trading_station.tooltip.progress": "Progress: %d%%"
|
||||
"trading_station.tooltip.progress": "Progress: %d%%",
|
||||
"trading_station.trading.recipe": "Trading recipe",
|
||||
"trading_station.trading_station.block.display": "Trading Station"
|
||||
}
|
||||
@@ -53,8 +53,9 @@ public class TradingStation
|
||||
Config.register();
|
||||
|
||||
registrate().addRawLang("itemGroup.trading_station:main", "Trading Station");
|
||||
registrate().addRawLang(ModLang.key("block.display"), "Trading Station");
|
||||
registrate().addRawLang(ModLang.key("recipe"), "Trading recipe");
|
||||
registrate().addRawLang(ModLang.key("trading_station.block.display"), "Trading Station");
|
||||
registrate().addRawLang(ModLang.key("powered_trading_station.block.display"), "Powered Trading Station");
|
||||
registrate().addRawLang(ModLang.key("trading.recipe"), "Trading recipe");
|
||||
registrate().addRawLang(ModLang.key("tooltip.progress"), "Progress: %d%%");
|
||||
registrate().addRawLang(ModLang.key("select_target.title"), "Select an output target");
|
||||
registrate().addRawLang(ModLang.key("select_target.button"), "Select target");
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.oierbravo.trading_station.compat.crafttweaker;
|
||||
import com.blamejared.crafttweaker.api.recipe.component.IDecomposedRecipe;
|
||||
import com.blamejared.crafttweaker.api.recipe.handler.IRecipeHandler;
|
||||
import com.blamejared.crafttweaker.api.recipe.manager.base.IRecipeManager;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingRecipe;
|
||||
import com.oierbravo.trading_station.content.trading_recipe.TradingRecipe;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.item.crafting.Recipe;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import com.blamejared.crafttweaker.api.fluid.IFluidStack;
|
||||
import com.blamejared.crafttweaker.api.ingredient.IIngredient;
|
||||
import com.blamejared.crafttweaker.api.recipe.manager.base.IRecipeManager;
|
||||
import com.oierbravo.trading_station.TradingStation;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingRecipe;
|
||||
import com.oierbravo.trading_station.content.trading_recipe.TradingRecipe;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.item.crafting.RecipeType;
|
||||
import org.openzen.zencode.java.ZenCodeType;
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package com.oierbravo.trading_station.compat.jei;
|
||||
|
||||
import com.oierbravo.trading_station.TradingStation;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingRecipe;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationScreen;
|
||||
import com.oierbravo.trading_station.content.trading_recipe.TradingRecipe;
|
||||
import com.oierbravo.trading_station.registrate.ModBlocks;
|
||||
import mezz.jei.api.IModPlugin;
|
||||
import mezz.jei.api.JeiPlugin;
|
||||
import mezz.jei.api.recipe.RecipeType;
|
||||
import mezz.jei.api.registration.IGuiHandlerRegistration;
|
||||
import mezz.jei.api.registration.IRecipeCatalystRegistration;
|
||||
import mezz.jei.api.registration.IRecipeCategoryRegistration;
|
||||
import mezz.jei.api.registration.IRecipeRegistration;
|
||||
@@ -36,6 +34,7 @@ public class JEIPlugin implements IModPlugin {
|
||||
@Override
|
||||
public void registerRecipeCatalysts(IRecipeCatalystRegistration registration) {
|
||||
registration.addRecipeCatalyst(new ItemStack(ModBlocks.TRADING_STATION.get()),new RecipeType<>(TradingRecipeCategory.UID, TradingRecipe.class));
|
||||
registration.addRecipeCatalyst(new ItemStack(ModBlocks.POWERED_TRADING_STATION.get()),new RecipeType<>(TradingRecipeCategory.UID, TradingRecipe.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import com.oierbravo.trading_station.TradingStation;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingRecipe;
|
||||
import com.oierbravo.trading_station.content.trading_recipe.TradingRecipe;
|
||||
import com.oierbravo.trading_station.foundation.util.ModLang;
|
||||
import com.oierbravo.trading_station.registrate.ModBlocks;
|
||||
import mezz.jei.api.constants.VanillaTypes;
|
||||
@@ -29,7 +29,6 @@ import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TradingRecipeCategory implements IRecipeCategory<TradingRecipe> {
|
||||
public final static ResourceLocation UID = new ResourceLocation(TradingStation.MODID, "trading");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.oierbravo.trading_station.compat.kubejs;
|
||||
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingRecipe;
|
||||
import com.oierbravo.trading_station.content.trading_recipe.TradingRecipe;
|
||||
import dev.latvian.mods.kubejs.recipe.schema.RegisterRecipeSchemasEvent;
|
||||
|
||||
public class KubeJSPlugin extends dev.latvian.mods.kubejs.KubeJSPlugin {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.oierbravo.trading_station.content.trading_station;
|
||||
package com.oierbravo.trading_station.content.trading_recipe;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.oierbravo.trading_station.TradingStation;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingRecipeBuilder.TradingRecipeParams;
|
||||
import com.oierbravo.trading_station.content.trading_recipe.TradingRecipeBuilder.TradingRecipeParams;
|
||||
import net.minecraft.core.NonNullList;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.oierbravo.trading_station.content.trading_station;
|
||||
package com.oierbravo.trading_station.content.trading_recipe;
|
||||
|
||||
import net.minecraft.core.NonNullList;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
@@ -1,416 +0,0 @@
|
||||
package com.oierbravo.trading_station.content.trading_station;
|
||||
|
||||
import com.oierbravo.trading_station.foundation.util.ModLang;
|
||||
import com.oierbravo.trading_station.network.packets.ItemStackSyncS2CPacket;
|
||||
import com.oierbravo.trading_station.registrate.ModMessages;
|
||||
import com.oierbravo.trading_station.registrate.ModRecipes;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.core.NonNullList;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.Containers;
|
||||
import net.minecraft.world.MenuProvider;
|
||||
import net.minecraft.world.SimpleContainer;
|
||||
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.ContainerData;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ForgeCapabilities;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.ItemStackHandler;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class AbstractTradingStationBlockEntity extends BlockEntity {
|
||||
private CompoundTag updateTag;
|
||||
public final ItemStackHandler inputItems = createInputItemHandler();
|
||||
public final ItemStackHandler outputItems = createOutputItemHandler();
|
||||
|
||||
public final ItemStackHandler targetItemHandler = createTargetItemHandler();
|
||||
private final LazyOptional<IItemHandler> inputItemHandler = LazyOptional.of(() -> inputItems);
|
||||
private final LazyOptional<IItemHandler> outputItemHandler = LazyOptional.of(() -> outputItems);
|
||||
|
||||
public int progress = 0;
|
||||
public int maxProgress = 1;
|
||||
private BlockState lastBlockState;
|
||||
|
||||
protected final ContainerData containerData;
|
||||
private Item preferedItem;
|
||||
|
||||
public AbstractTradingStationBlockEntity(BlockEntityType<?> pType, BlockPos pWorldPosition, BlockState pBlockState) {
|
||||
super(pType, pWorldPosition, pBlockState);
|
||||
updateTag = getPersistentData();
|
||||
lastBlockState = this.getBlockState();
|
||||
preferedItem = ItemStack.EMPTY.getItem();
|
||||
containerData = AbstractTradingStationBlockEntity.createContainerData(this);
|
||||
}
|
||||
public static ContainerData createContainerData(AbstractTradingStationBlockEntity pBlockEntity){
|
||||
return new ContainerData(){
|
||||
@Override
|
||||
public int get(int pIndex){
|
||||
return switch (pIndex) {
|
||||
case 0 -> pBlockEntity.progress;
|
||||
case 1 -> pBlockEntity.maxProgress;
|
||||
default -> 0;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void set(int pIndex, int pValue) {
|
||||
switch (pIndex) {
|
||||
case 0 -> pBlockEntity.progress = pValue;
|
||||
case 1 -> pBlockEntity.maxProgress = pValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return 2;
|
||||
}
|
||||
};
|
||||
}
|
||||
@NotNull
|
||||
@Nonnull
|
||||
private ItemStackHandler createTargetItemHandler() {
|
||||
return new ItemStackHandler(1) {
|
||||
@Override
|
||||
protected void onContentsChanged(int slot) {
|
||||
setChanged();
|
||||
if(!level.isClientSide()) {
|
||||
ModMessages.sendToClients(new ItemStackSyncS2CPacket(slot,this.getStackInSlot(0), worldPosition, ItemStackSyncS2CPacket.SlotType.TARGET));
|
||||
}
|
||||
if(!level.isClientSide()) {
|
||||
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
|
||||
}
|
||||
// clientSync();
|
||||
}
|
||||
@Override
|
||||
public boolean isItemValid(int slot, ItemStack stack) {
|
||||
return true;
|
||||
//return canProcess(stack) && super.isItemValid(slot, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ItemStack extractItem(int slot, int amount, boolean simulate) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private ItemStackHandler createInputItemHandler() {
|
||||
return new ItemStackHandler(2) {
|
||||
@Override
|
||||
protected void onContentsChanged(int slot) {
|
||||
setChanged();
|
||||
if(!level.isClientSide()) {
|
||||
ModMessages.sendToClients(new ItemStackSyncS2CPacket(slot,this.getStackInSlot(0), worldPosition, ItemStackSyncS2CPacket.SlotType.INPUT));
|
||||
}
|
||||
if(!level.isClientSide()) {
|
||||
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
|
||||
}
|
||||
// clientSync();
|
||||
}
|
||||
@Override
|
||||
public boolean isItemValid(int slot, ItemStack stack) {
|
||||
return true;
|
||||
//return canProcess(stack) && super.isItemValid(slot, stack);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Nonnull
|
||||
private ItemStackHandler createOutputItemHandler() {
|
||||
return new ItemStackHandler(1) {
|
||||
@Override
|
||||
protected void onContentsChanged(int slot) {
|
||||
setChanged();
|
||||
if(!level.isClientSide()) {
|
||||
ModMessages.sendToClients(new ItemStackSyncS2CPacket(slot, this.getStackInSlot(slot), worldPosition, ItemStackSyncS2CPacket.SlotType.OUTPUT));
|
||||
}
|
||||
if(!level.isClientSide()) {
|
||||
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
|
||||
}
|
||||
// clientSync();
|
||||
}
|
||||
@Override
|
||||
public boolean isItemValid(int slot, ItemStack stack) {
|
||||
return canProcess(stack) && super.isItemValid(slot, stack);
|
||||
//return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
@Override
|
||||
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
|
||||
if (cap == ForgeCapabilities.ITEM_HANDLER) {
|
||||
return inputItemHandler.cast();
|
||||
}
|
||||
return super.getCapability(cap, side);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRemoved() {
|
||||
super.setRemoved();
|
||||
inputItemHandler.invalidate();
|
||||
outputItemHandler.invalidate();
|
||||
}
|
||||
public LazyOptional<IItemHandler> getInputItemHandler(){
|
||||
return inputItemHandler;
|
||||
}
|
||||
public LazyOptional<IItemHandler> getOutputItemHandler(){
|
||||
return outputItemHandler;
|
||||
}
|
||||
@Override
|
||||
public void onLoad() {
|
||||
super.onLoad();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidateCaps() {
|
||||
super.invalidateCaps();
|
||||
inputItemHandler.invalidate();
|
||||
outputItemHandler.invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void saveAdditional(@NotNull CompoundTag tag) {
|
||||
super.saveAdditional(tag);
|
||||
tag.put("input", inputItems.serializeNBT());
|
||||
tag.put("output", outputItems.serializeNBT());
|
||||
tag.put("target", targetItemHandler.serializeNBT());
|
||||
tag.putInt("trading_station.progress", progress);
|
||||
tag.putInt("trading_station.maxProgress", maxProgress);
|
||||
String preferedItemString = ForgeRegistries.ITEMS.getKey(preferedItem).toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(CompoundTag tag) {
|
||||
super.load(tag);
|
||||
inputItems.deserializeNBT(tag.getCompound("input"));
|
||||
outputItems.deserializeNBT(tag.getCompound("output"));
|
||||
targetItemHandler.deserializeNBT(tag.getCompound("target"));
|
||||
progress = tag.getInt("trading_station.progress");
|
||||
maxProgress = tag.getInt("trading_station.maxProgress");
|
||||
String preferedItemString = tag.getString("trading_station.preferedItem");
|
||||
preferedItem = ForgeRegistries.ITEMS.getValue( ResourceLocation.tryParse(preferedItemString));
|
||||
}
|
||||
|
||||
public void drops() {
|
||||
SimpleContainer inventory = new SimpleContainer(inputItems.getSlots() + 1);
|
||||
for (int i = 0; i < inputItems.getSlots(); i++) {
|
||||
inventory.setItem(i, inputItems.getStackInSlot(i));
|
||||
}
|
||||
inventory.setItem(inputItems.getSlots(), inputItems.getStackInSlot(0));
|
||||
|
||||
Containers.dropContents(this.level, this.worldPosition, inventory);
|
||||
}
|
||||
|
||||
|
||||
public void resetProgress() {
|
||||
this.progress = 0;
|
||||
this.maxProgress = 1;
|
||||
}
|
||||
|
||||
protected static Optional<TradingRecipe> getRecipe(AbstractTradingStationBlockEntity pBlockEntity){
|
||||
Level level = pBlockEntity.getLevel();
|
||||
SimpleContainer inputInventory = getInputInventory(pBlockEntity);
|
||||
if(!pBlockEntity.targetItemHandler.getStackInSlot(0).isEmpty())
|
||||
return ModRecipes.findByOutput(level,pBlockEntity.targetItemHandler.getStackInSlot(0));
|
||||
return ModRecipes.find(inputInventory,level);
|
||||
// return level.getRecipeManager().getRecipeFor(TradingRecipe.Type.INSTANCE, inputInventory, level);
|
||||
}
|
||||
protected int getProcessingTime(AbstractTradingStationBlockEntity pBlockEntity) {
|
||||
return getRecipe(pBlockEntity).map(TradingRecipe::getProcessingTime).orElse(1);
|
||||
}
|
||||
|
||||
public static void tick(Level pLevel, BlockPos pPos, BlockState pState, AbstractTradingStationBlockEntity pBlockEntity) {
|
||||
|
||||
if(pLevel.isClientSide()) {
|
||||
return;
|
||||
}
|
||||
if(!isPowered(pBlockEntity))
|
||||
return;
|
||||
|
||||
if (canCraftItem(pBlockEntity)) {
|
||||
pBlockEntity.progress += 1;
|
||||
BlockEntity.setChanged(pLevel, pPos, pState);
|
||||
pBlockEntity.maxProgress = pBlockEntity.getProcessingTime(pBlockEntity);
|
||||
if (pBlockEntity.progress > pBlockEntity.maxProgress) {
|
||||
AbstractTradingStationBlockEntity.craftItem(pBlockEntity);
|
||||
}
|
||||
BlockEntity.setChanged(pLevel, pPos, pState);
|
||||
|
||||
} else {
|
||||
pBlockEntity.resetProgress();
|
||||
BlockEntity.setChanged(pLevel, pPos, pState);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
protected static void updateProgress(AbstractTradingStationBlockEntity pBlockEntity){
|
||||
pBlockEntity.progress += 1;
|
||||
|
||||
}
|
||||
private static boolean isPowered(AbstractTradingStationBlockEntity pBlockEntity){
|
||||
return pBlockEntity.getLevel().getBlockState(pBlockEntity.getBlockPos())
|
||||
.getValue(BlockStateProperties.POWERED);
|
||||
}
|
||||
private static SimpleContainer getInputInventory(AbstractTradingStationBlockEntity pBlockEntity){
|
||||
int containerSize = 0;
|
||||
for(int index = 0; index < pBlockEntity.inputItems.getSlots(); index++) {
|
||||
if (!pBlockEntity.inputItems.getStackInSlot(index).isEmpty())
|
||||
containerSize++;
|
||||
}
|
||||
|
||||
SimpleContainer inputInventory = new SimpleContainer(containerSize);
|
||||
pBlockEntity.inputItemHandler.ifPresent(iItemHandler -> {
|
||||
for(int slot = 0; slot < iItemHandler.getSlots(); slot++) {
|
||||
if(!iItemHandler.getStackInSlot(slot).isEmpty()){
|
||||
inputInventory.addItem(iItemHandler.getStackInSlot(slot));
|
||||
}
|
||||
}
|
||||
});
|
||||
return inputInventory;
|
||||
}
|
||||
private static void craftItem(AbstractTradingStationBlockEntity pBlockEntity) {
|
||||
Level level = pBlockEntity.getLevel();
|
||||
SimpleContainer inputInventory = getInputInventory(pBlockEntity);
|
||||
|
||||
Optional<TradingRecipe> recipe = getRecipe(pBlockEntity);
|
||||
|
||||
if(recipe.isPresent()){
|
||||
for (int i = 0; i < recipe.get().getIngredients().size(); i++) {
|
||||
Ingredient ingredient = recipe.get().getIngredients().get(i);
|
||||
|
||||
for (int slot = 0; slot < pBlockEntity.inputItems.getSlots(); slot++) {
|
||||
ItemStack itemStack = pBlockEntity.inputItems.getStackInSlot(slot);
|
||||
if(ingredient.test(itemStack)){
|
||||
pBlockEntity.inputItems.extractItem(slot,ingredient.getItems()[0].getCount(),false);
|
||||
inputInventory.setChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
pBlockEntity.outputItems.insertItem(0, recipe.get().getResultItem(), false);
|
||||
}
|
||||
|
||||
pBlockEntity.resetProgress();
|
||||
pBlockEntity.setChanged();
|
||||
}
|
||||
|
||||
|
||||
static boolean canCraftItem(AbstractTradingStationBlockEntity pBlockEntity) {
|
||||
Level level = pBlockEntity.getLevel();
|
||||
if(level == null)
|
||||
return false;
|
||||
|
||||
SimpleContainer inputInventory = getInputInventory(pBlockEntity);
|
||||
|
||||
Optional<TradingRecipe> match = getRecipe(pBlockEntity);
|
||||
|
||||
if(match.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
//return false;
|
||||
return match.isPresent()
|
||||
&& AbstractTradingStationBlockEntity.hasEnoughInputItems(inputInventory,match.get().getIngredients())
|
||||
&& AbstractTradingStationBlockEntity.hasEnoughOutputSpace(pBlockEntity.outputItems,match.get().getResultItem());
|
||||
}
|
||||
|
||||
private boolean canProcess(ItemStack stack) {
|
||||
|
||||
return getRecipe(this).isPresent();
|
||||
}
|
||||
protected static boolean hasEnoughInputItems(SimpleContainer inventory, NonNullList<Ingredient> ingredients){
|
||||
int enough = 0;
|
||||
for(int ingredientIndex = 0; ingredientIndex < ingredients.size();ingredientIndex ++){
|
||||
Ingredient ingredient = ingredients.get(ingredientIndex);
|
||||
for(int slot = 0; slot < inventory.getContainerSize(); slot++){
|
||||
if(ingredient.test(inventory.getItem(slot))){
|
||||
if(inventory.getItem(slot).getCount() >= ingredient.getItems()[0].getCount() )
|
||||
enough++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ingredients.size() == enough;
|
||||
}
|
||||
|
||||
protected static boolean hasEnoughOutputSpace(ItemStackHandler stackHandler,ItemStack resultItemStack){
|
||||
return stackHandler.getStackInSlot(0).isEmpty() || stackHandler.getStackInSlot(0).is(resultItemStack.getItem()) && stackHandler.getStackInSlot(0).getMaxStackSize() - stackHandler.getStackInSlot(0).getCount() >= resultItemStack.getCount() ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag getUpdateTag() {
|
||||
this.saveAdditional(updateTag);
|
||||
return updateTag;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public ClientboundBlockEntityDataPacket getUpdatePacket() {
|
||||
return ClientboundBlockEntityDataPacket.create(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleUpdateTag(CompoundTag tag) {
|
||||
this.load(tag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt) {
|
||||
this.load(pkt.getTag());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getProgressPercent() {
|
||||
return this.progress * 100 / this.maxProgress;
|
||||
}
|
||||
|
||||
|
||||
public void setItemStack(int slot, ItemStack itemStack,ItemStackSyncS2CPacket.SlotType slotType) {
|
||||
if(slotType == ItemStackSyncS2CPacket.SlotType.INPUT)
|
||||
inputItems.setStackInSlot(slot,itemStack);
|
||||
else if (slotType == ItemStackSyncS2CPacket.SlotType.OUTPUT)
|
||||
outputItems.setStackInSlot(slot,itemStack);
|
||||
else if (slotType == ItemStackSyncS2CPacket.SlotType.TARGET)
|
||||
targetItemHandler.setStackInSlot(slot,itemStack);
|
||||
|
||||
}
|
||||
|
||||
public void setPreferedItem(ItemStack itemStack) {
|
||||
this.targetItemHandler.setStackInSlot(0,itemStack);
|
||||
}
|
||||
|
||||
|
||||
public ItemStack getPreferedItemStack() {
|
||||
return targetItemHandler.getStackInSlot(0);
|
||||
}
|
||||
public ItemStackHandler getTargetItemHandler(){
|
||||
return targetItemHandler;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.oierbravo.trading_station.content.trading_station;
|
||||
|
||||
import com.oierbravo.trading_station.network.packets.ItemStackSyncS2CPacket;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
public interface ITradingStationBlockEntity {
|
||||
public int progress = 0;
|
||||
public int maxProgress = 1;
|
||||
LazyOptional<IItemHandler> getInputItemHandler();
|
||||
LazyOptional<IItemHandler> getOutputItemHandler();
|
||||
|
||||
IItemHandler getTargetItemHandler();
|
||||
|
||||
BlockPos getBlockPos();
|
||||
|
||||
ItemStack getTargetItemStack();
|
||||
|
||||
Level getLevel();
|
||||
|
||||
void setPreferedItem(ItemStack itemStack);
|
||||
|
||||
void setItemStack(int slot, ItemStack itemStack, ItemStackSyncS2CPacket.SlotType slotType);
|
||||
|
||||
|
||||
IEnergyStorage getEnergyStorage();
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.MenuProvider;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.context.BlockPlaceContext;
|
||||
import net.minecraft.world.level.BlockGetter;
|
||||
@@ -91,8 +92,11 @@ public class TradingStationBlock extends BaseEntityBlock {
|
||||
@Nullable
|
||||
@Override
|
||||
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) {
|
||||
if(pLevel.isClientSide()) {
|
||||
return null;
|
||||
}
|
||||
return createTickerHelper(pBlockEntityType, ModBlockEntities.TRADING_STATION.get(),
|
||||
TradingStationBlockEntity::tick);
|
||||
(pLevel1, pPos, pState1, pBlockEntity) -> pBlockEntity.tick(pLevel1, pPos, pState1));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -119,11 +123,10 @@ public class TradingStationBlock extends BaseEntityBlock {
|
||||
|
||||
@Override
|
||||
public InteractionResult use(BlockState state, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand handIn, BlockHitResult hit) {
|
||||
|
||||
if (!pLevel.isClientSide()) {
|
||||
BlockEntity blockEntity = pLevel.getBlockEntity(pPos);
|
||||
if(blockEntity instanceof TradingStationBlockEntity) {
|
||||
NetworkHooks.openScreen(((ServerPlayer)pPlayer), (TradingStationBlockEntity)blockEntity, pPos);
|
||||
NetworkHooks.openScreen(((ServerPlayer)pPlayer), (TradingStationBlockEntity) blockEntity, pPos);
|
||||
} else {
|
||||
throw new IllegalStateException("Our Container provider is missing!");
|
||||
}
|
||||
@@ -132,4 +135,6 @@ public class TradingStationBlock extends BaseEntityBlock {
|
||||
return InteractionResult.sidedSuccess(pLevel.isClientSide());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.oierbravo.trading_station.content.trading_station;
|
||||
|
||||
import com.oierbravo.trading_station.content.trading_recipe.TradingRecipe;
|
||||
import com.oierbravo.trading_station.foundation.util.ModLang;
|
||||
import com.oierbravo.trading_station.network.packets.ItemStackSyncS2CPacket;
|
||||
import com.oierbravo.trading_station.registrate.ModMessages;
|
||||
@@ -22,7 +23,6 @@ import net.minecraft.world.inventory.ContainerData;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
import net.minecraft.world.item.crafting.ShapelessRecipe;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
@@ -31,6 +31,7 @@ import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ForgeCapabilities;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.ItemStackHandler;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
@@ -38,15 +39,11 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class TradingStationBlockEntity extends BlockEntity implements MenuProvider {
|
||||
public class TradingStationBlockEntity extends BlockEntity implements MenuProvider, ITradingStationBlockEntity {
|
||||
|
||||
|
||||
//private final int FLUID_CAPACITY = TradingStationConfig.trading_station_CAPACITY.get();
|
||||
//private static final int FLIUD_PER_TICK = TradingStationConfig.trading_statio_FLUID_PER_TICK.get();
|
||||
private CompoundTag updateTag;
|
||||
public final ItemStackHandler inputItems = createInputItemHandler();
|
||||
public final ItemStackHandler outputItems = createOutputItemHandler();
|
||||
@@ -139,12 +136,10 @@ public class TradingStationBlockEntity extends BlockEntity implements MenuProvi
|
||||
if(!level.isClientSide()) {
|
||||
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
|
||||
}
|
||||
// clientSync();
|
||||
}
|
||||
@Override
|
||||
public boolean isItemValid(int slot, ItemStack stack) {
|
||||
return true;
|
||||
//return canProcess(stack) && super.isItemValid(slot, stack);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -254,32 +249,32 @@ public class TradingStationBlockEntity extends BlockEntity implements MenuProvi
|
||||
return getRecipe(pBlockEntity).map(TradingRecipe::getProcessingTime).orElse(1);
|
||||
}
|
||||
|
||||
public static void tick(Level pLevel, BlockPos pPos, BlockState pState, TradingStationBlockEntity pBlockEntity) {
|
||||
public void tick(Level pLevel, BlockPos pPos, BlockState pState) {
|
||||
|
||||
if(pLevel.isClientSide()) {
|
||||
return;
|
||||
}
|
||||
if(!isPowered(pBlockEntity))
|
||||
if(!isPowered(this))
|
||||
return;
|
||||
|
||||
if (canCraftItem(pBlockEntity)) {
|
||||
pBlockEntity.progress += 1;
|
||||
if (canCraftItem()) {
|
||||
this.updateProgress();
|
||||
BlockEntity.setChanged(pLevel, pPos, pState);
|
||||
pBlockEntity.maxProgress = pBlockEntity.getProcessingTime(pBlockEntity);
|
||||
if (pBlockEntity.progress > pBlockEntity.maxProgress) {
|
||||
TradingStationBlockEntity.craftItem(pBlockEntity);
|
||||
this.maxProgress = this.getProcessingTime(this);
|
||||
if (this.progress > this.maxProgress) {
|
||||
craftItem();
|
||||
}
|
||||
BlockEntity.setChanged(pLevel, pPos, pState);
|
||||
|
||||
} else {
|
||||
pBlockEntity.resetProgress();
|
||||
this.resetProgress();
|
||||
BlockEntity.setChanged(pLevel, pPos, pState);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
protected static void updateProgress(TradingStationBlockEntity pBlockEntity){
|
||||
pBlockEntity.progress += 1;
|
||||
protected void updateProgress(){
|
||||
this.progress += 1;
|
||||
|
||||
}
|
||||
private static boolean isPowered(TradingStationBlockEntity pBlockEntity){
|
||||
@@ -303,40 +298,40 @@ public class TradingStationBlockEntity extends BlockEntity implements MenuProvi
|
||||
});
|
||||
return inputInventory;
|
||||
}
|
||||
private static void craftItem(TradingStationBlockEntity pBlockEntity) {
|
||||
Level level = pBlockEntity.getLevel();
|
||||
SimpleContainer inputInventory = getInputInventory(pBlockEntity);
|
||||
private void craftItem() {
|
||||
Level level = this.getLevel();
|
||||
SimpleContainer inputInventory = getInputInventory(this);
|
||||
|
||||
Optional<TradingRecipe> recipe = getRecipe(pBlockEntity);
|
||||
Optional<TradingRecipe> recipe = getRecipe(this);
|
||||
|
||||
if(recipe.isPresent()){
|
||||
for (int i = 0; i < recipe.get().getIngredients().size(); i++) {
|
||||
Ingredient ingredient = recipe.get().getIngredients().get(i);
|
||||
|
||||
for (int slot = 0; slot < pBlockEntity.inputItems.getSlots(); slot++) {
|
||||
ItemStack itemStack = pBlockEntity.inputItems.getStackInSlot(slot);
|
||||
for (int slot = 0; slot < this.inputItems.getSlots(); slot++) {
|
||||
ItemStack itemStack = this.inputItems.getStackInSlot(slot);
|
||||
if(ingredient.test(itemStack)){
|
||||
pBlockEntity.inputItems.extractItem(slot,ingredient.getItems()[0].getCount(),false);
|
||||
this.inputItems.extractItem(slot,ingredient.getItems()[0].getCount(),false);
|
||||
inputInventory.setChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
pBlockEntity.outputItems.insertItem(0, recipe.get().getResultItem(), false);
|
||||
this.outputItems.insertItem(0, recipe.get().getResultItem(), false);
|
||||
}
|
||||
|
||||
pBlockEntity.resetProgress();
|
||||
pBlockEntity.setChanged();
|
||||
this.resetProgress();
|
||||
this.setChanged();
|
||||
}
|
||||
|
||||
|
||||
static boolean canCraftItem(TradingStationBlockEntity pBlockEntity) {
|
||||
Level level = pBlockEntity.getLevel();
|
||||
protected boolean canCraftItem() {
|
||||
Level level = this.getLevel();
|
||||
if(level == null)
|
||||
return false;
|
||||
|
||||
SimpleContainer inputInventory = getInputInventory(pBlockEntity);
|
||||
SimpleContainer inputInventory = getInputInventory(this);
|
||||
|
||||
Optional<TradingRecipe> match = getRecipe(pBlockEntity);
|
||||
Optional<TradingRecipe> match = getRecipe(this);
|
||||
|
||||
if(match.isEmpty()) {
|
||||
return false;
|
||||
@@ -344,7 +339,7 @@ public class TradingStationBlockEntity extends BlockEntity implements MenuProvi
|
||||
//return false;
|
||||
return match.isPresent()
|
||||
&& TradingStationBlockEntity.hasEnoughInputItems(inputInventory,match.get().getIngredients())
|
||||
&& TradingStationBlockEntity.hasEnoughOutputSpace(pBlockEntity.outputItems,match.get().getResultItem());
|
||||
&& TradingStationBlockEntity.hasEnoughOutputSpace(this.outputItems,match.get().getResultItem());
|
||||
}
|
||||
|
||||
private boolean canProcess(ItemStack stack) {
|
||||
@@ -408,6 +403,11 @@ public class TradingStationBlockEntity extends BlockEntity implements MenuProvi
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEnergyStorage getEnergyStorage() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component getDisplayName() {
|
||||
return ModLang.translate("block.display");
|
||||
@@ -424,11 +424,8 @@ public class TradingStationBlockEntity extends BlockEntity implements MenuProvi
|
||||
}
|
||||
|
||||
|
||||
public ItemStack getPreferedItemStack() {
|
||||
public ItemStack getTargetItemStack() {
|
||||
return targetItemHandler.getStackInSlot(0);
|
||||
}
|
||||
public ItemStackHandler getTargetItemHandler(){
|
||||
return targetItemHandler;
|
||||
|
||||
}
|
||||
public ItemStackHandler getTargetItemHandler(){ return targetItemHandler;}
|
||||
}
|
||||
|
||||
@@ -8,19 +8,23 @@ import net.minecraft.client.renderer.block.model.ItemTransforms;
|
||||
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
|
||||
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class TradingStationBlockRenderer implements BlockEntityRenderer<TradingStationBlockEntity> {
|
||||
public class TradingStationBlockRenderer<TSBE extends BlockEntity> implements BlockEntityRenderer<TSBE> {
|
||||
public TradingStationBlockRenderer(BlockEntityRendererProvider.Context context) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(TradingStationBlockEntity pBlockEntity, float pPartialTick, @NotNull PoseStack pPoseStack, @NotNull MultiBufferSource pBufferSource, int pPackedLight, int pPackedOverlay) {
|
||||
if(!pBlockEntity.getPreferedItemStack().isEmpty()){
|
||||
pPoseStack.pushPose();
|
||||
pPoseStack.translate(0.5d, 1.1d, 0.5d);
|
||||
renderBlock(pPoseStack,pBufferSource, LightTexture.FULL_BRIGHT,pPackedOverlay,pBlockEntity.getPreferedItemStack());
|
||||
pPoseStack.popPose();
|
||||
public void render(BlockEntity pBlockEntity, float pPartialTick, @NotNull PoseStack pPoseStack, @NotNull MultiBufferSource pBufferSource, int pPackedLight, int pPackedOverlay) {
|
||||
if(pBlockEntity instanceof ITradingStationBlockEntity) {
|
||||
ITradingStationBlockEntity blockEntity = (ITradingStationBlockEntity) pBlockEntity;
|
||||
if (!blockEntity.getTargetItemStack().isEmpty()) {
|
||||
pPoseStack.pushPose();
|
||||
pPoseStack.translate(0.5d, 1.1d, 0.5d);
|
||||
renderBlock(pPoseStack, pBufferSource, LightTexture.FULL_BRIGHT, pPackedOverlay, blockEntity.getTargetItemStack());
|
||||
pPoseStack.popPose();
|
||||
}
|
||||
}
|
||||
}
|
||||
protected void renderBlock(PoseStack ms, MultiBufferSource buffer, int light, int overlay, ItemStack stack) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.oierbravo.trading_station.content.trading_station;
|
||||
|
||||
import com.oierbravo.trading_station.foundation.gui.AbstractTradingMenu;
|
||||
import com.oierbravo.trading_station.registrate.ModBlocks;
|
||||
import com.oierbravo.trading_station.registrate.ModMenus;
|
||||
import net.minecraft.client.Minecraft;
|
||||
@@ -18,125 +19,30 @@ import net.minecraftforge.items.ItemStackHandler;
|
||||
import net.minecraftforge.items.SlotItemHandler;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class TradingStationMenu extends AbstractContainerMenu {
|
||||
public final TradingStationBlockEntity blockEntity;
|
||||
public final Inventory inventory;
|
||||
private final Level level;
|
||||
private final ContainerData containerData;
|
||||
public ItemStackHandler ghostInventory;
|
||||
public class TradingStationMenu extends AbstractTradingMenu {
|
||||
protected final int[] outputSlotCoords = {131,38};
|
||||
|
||||
|
||||
public TradingStationMenu(int pContainerId, Inventory inv, FriendlyByteBuf extraData){
|
||||
this(pContainerId, inv, inv.player.level.getBlockEntity(extraData.readBlockPos()), new SimpleContainerData(2));
|
||||
public TradingStationMenu(int pContainerId, Inventory pInv, BlockEntity pBlockEntity, ContainerData pData) {
|
||||
super(ModMenus.TRADING_STATION.get(), pContainerId, pInv, pBlockEntity, pData);
|
||||
}
|
||||
|
||||
public TradingStationMenu(int pContainerId, Inventory pInv, BlockEntity pBlockEntity, ContainerData pData){
|
||||
super(ModMenus.TRADING_STATION.get(), pContainerId);
|
||||
checkContainerSize(pInv, 3);
|
||||
blockEntity = (TradingStationBlockEntity) pBlockEntity;
|
||||
this.level = pInv.player.getLevel();
|
||||
this.containerData = pData;
|
||||
this.inventory = pInv;
|
||||
addPlayerHotbar(pInv);
|
||||
addPlayerInventory(pInv);
|
||||
this.addDataSlots(pData);
|
||||
//this.blockEntity.inputItems
|
||||
this.blockEntity.getInputItemHandler().ifPresent(itemHandler -> {
|
||||
this.addSlot(new SlotItemHandler(itemHandler,0,TradingStationScreen.inputSlotX[0],TradingStationScreen.inputSlotY));
|
||||
this.addSlot(new SlotItemHandler(itemHandler,1,TradingStationScreen.inputSlotX[1],TradingStationScreen.inputSlotY));
|
||||
});
|
||||
this.blockEntity.getOutputItemHandler().ifPresent(itemHandler -> {
|
||||
this.addSlot(new SlotItemHandler(itemHandler,0,TradingStationScreen.outputSlotX,TradingStationScreen.outputSlotY));
|
||||
});
|
||||
this.addSlot(new SlotItemHandler(this.blockEntity.getTargetItemHandler(),0,87,28));
|
||||
|
||||
|
||||
}
|
||||
|
||||
public TradingStationMenu(int pContainerId, Inventory pInventory, BlockPos sourcePos) {
|
||||
this(pContainerId, pInventory, Minecraft.getInstance().level.getBlockEntity(sourcePos), TradingStationBlockEntity.createContainerData((TradingStationBlockEntity) Minecraft.getInstance().level.getBlockEntity(sourcePos)));
|
||||
public TradingStationMenu(int pContainerId, Inventory inventory, FriendlyByteBuf buf) {
|
||||
super(ModMenus.TRADING_STATION.get(), pContainerId, inventory, buf);
|
||||
}
|
||||
|
||||
public static TradingStationMenu factory(@Nullable MenuType<TradingStationMenu> pMenuType, int pContainerId, Inventory inventory, FriendlyByteBuf buf) {
|
||||
return new TradingStationMenu(pContainerId, inventory, buf);
|
||||
}
|
||||
public int getScaledProgress() {
|
||||
int progress = this.containerData.get(0);
|
||||
int maxProgress = this.containerData.get(1); // Max Progress
|
||||
int progressArrowSize = 34; // This is the height in pixels of your arrow
|
||||
|
||||
return maxProgress != 0 && progress != 0 ? progress * progressArrowSize / maxProgress : 0;
|
||||
}
|
||||
public boolean isCrafting() {
|
||||
return containerData.get(0) > 0;
|
||||
}
|
||||
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 = 2; // must be the number of slots you have!
|
||||
@Override
|
||||
public ItemStack quickMoveStack(Player playerIn, int pIndex) {
|
||||
Slot sourceSlot = slots.get(pIndex);
|
||||
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 (pIndex < 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 (pIndex < 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 if (pIndex == TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT) {
|
||||
// This is a TE Outpu 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:" + pIndex);
|
||||
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 pPlayer) {
|
||||
return stillValid(ContainerLevelAccess.create(level, blockEntity.getBlockPos()), pPlayer, ModBlocks.TRADING_STATION.get());
|
||||
}
|
||||
private static final int PLAYER_INVENTORY_Y = 64;
|
||||
|
||||
private void addPlayerInventory(Inventory playerInventory) {
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int l = 0; l < 9; ++l) {
|
||||
this.addSlot(new Slot(playerInventory, l + i * 9 + 9, 8 + l * 18, PLAYER_INVENTORY_Y + i * 18));
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public int[][] getInputSlotCoords() {
|
||||
return new int[][]{{19,38},{42,38}};
|
||||
}
|
||||
private static final int HOTBAR_Y = 122;
|
||||
|
||||
private void addPlayerHotbar(Inventory playerInventory) {
|
||||
for (int i = 0; i < 9; ++i) {
|
||||
this.addSlot(new Slot(playerInventory, i, 8 + i * 18, HOTBAR_Y));
|
||||
}
|
||||
@Override
|
||||
public int[] getOutputSlotCoords() {
|
||||
return new int[]{131,38};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ package com.oierbravo.trading_station.content.trading_station;
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import com.oierbravo.trading_station.TradingStation;
|
||||
import com.oierbravo.trading_station.content.trading_recipe.TradingRecipe;
|
||||
import com.oierbravo.trading_station.foundation.gui.AbstractTradingScreen;
|
||||
import com.oierbravo.trading_station.foundation.render.FakeItemRenderer;
|
||||
import com.oierbravo.trading_station.foundation.util.ModLang;
|
||||
import com.oierbravo.trading_station.registrate.ModRecipes;
|
||||
import net.minecraft.client.Minecraft;
|
||||
@@ -11,81 +14,37 @@ 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.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
import net.minecraftforge.client.gui.widget.ExtendedButton;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
|
||||
public class TradingStationScreen extends AbstractContainerScreen<TradingStationMenu> {
|
||||
public class TradingStationScreen extends AbstractTradingScreen<TradingStationMenu> {
|
||||
|
||||
protected static int[] progressArrowCoords = {79,47};
|
||||
protected static int[] targetSelectButtonCoords = {131,18};
|
||||
private static final ResourceLocation TEXTURE = TradingStation.asResource("textures/gui/trading_station.png");
|
||||
|
||||
private int progressArrowX = 79;
|
||||
private int progressArrowY = 47;
|
||||
|
||||
private int targetSelectButtonX = 131;
|
||||
private int targetSelectButtonY = 18;
|
||||
|
||||
public static int inputSlotX[] = {19,42};
|
||||
public static int inputSlotY = 38;
|
||||
public static int outputSlotX = 131;
|
||||
public static int outputSlotY = 38;
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
super.init();
|
||||
this.titleLabelX = 4;
|
||||
this.titleLabelY = 4;
|
||||
this.inventoryLabelY = 100000;
|
||||
this.addRenderableWidget(new ExtendedButton(leftPos + targetSelectButtonX,topPos + targetSelectButtonY,16,16, ModLang.translate("select_target.button"), btn ->{
|
||||
TradingStationTargetSelectScreen screen = new TradingStationTargetSelectScreen( this.menu.blockEntity);
|
||||
Minecraft.getInstance().pushGuiLayer(screen);
|
||||
|
||||
}));
|
||||
}
|
||||
|
||||
public TradingStationScreen(TradingStationMenu pMenu, Inventory pPlayerInventory, Component pTitle) {
|
||||
super(pMenu, pPlayerInventory, pTitle);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected void renderBg(PoseStack pPoseStack, float pPartialTick, int pMouseX, int pMouseY) {
|
||||
RenderSystem.setShader(GameRenderer::getPositionTexShader);
|
||||
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
RenderSystem.setShaderTexture(0, TEXTURE);
|
||||
this.fillGradient(pPoseStack, 0, 0, this.width, this.height, -1072689136, -804253680);
|
||||
|
||||
int x = (width - imageWidth) / 2;
|
||||
int y = (height - imageHeight) / 2;
|
||||
this.blit(pPoseStack, x, y, 0, 0, imageWidth, imageHeight);
|
||||
|
||||
if (menu.isCrafting()) {
|
||||
this.blit(pPoseStack, x + progressArrowX, y + progressArrowY, 179, 0, menu.getScaledProgress(), 7);
|
||||
}
|
||||
|
||||
if(!menu.blockEntity.getPreferedItemStack().isEmpty()){
|
||||
Optional<TradingRecipe> recipe = ModRecipes.findByOutput(menu.blockEntity.getLevel(),menu.blockEntity.getPreferedItemStack());
|
||||
if(recipe.isPresent()){
|
||||
renderFakeRecipe(recipe.get(), pPoseStack);
|
||||
}
|
||||
}
|
||||
public int[] getProgressArrowCoords() {
|
||||
return progressArrowCoords;
|
||||
}
|
||||
private void renderFakeRecipe(TradingRecipe recipe, PoseStack pPoseStack){
|
||||
for(int index = 0; index < recipe.getIngredients().size(); index++){
|
||||
Ingredient ingredient = recipe.getIngredients().get(index);
|
||||
if(!ingredient.isEmpty())
|
||||
FakeItemRenderer.renderFakeItem(ingredient.getItems()[0],getGuiLeft() + TradingStationScreen.inputSlotX[index],getGuiTop() + TradingStationScreen.inputSlotY, .5f);
|
||||
}
|
||||
FakeItemRenderer.renderFakeItem(recipe.getResultItem(),getGuiLeft() + TradingStationScreen.outputSlotX,getGuiTop() + TradingStationScreen.outputSlotY, .5f);
|
||||
|
||||
@Override
|
||||
protected int[] getTargetSelectButtonCoords() {
|
||||
return targetSelectButtonCoords;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void render(PoseStack pPoseStack, int pMouseX, int pMouseY, float pPartialTick) {
|
||||
super.render(pPoseStack, pMouseX, pMouseY, pPartialTick);
|
||||
protected ResourceLocation getTexture() {
|
||||
return TEXTURE;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -26,7 +26,7 @@ import java.util.List;
|
||||
*/
|
||||
public class TradingStationTargetSelectScreen extends Screen {
|
||||
private static final ResourceLocation TEXTURE = TradingStation.asResource("textures/gui/trade_select.png");
|
||||
private TradingStationBlockEntity blockEntity;
|
||||
private ITradingStationBlockEntity blockEntity;
|
||||
private List<ItemStack> allPossibleOutputs;
|
||||
private LinkedList<ItemStack> displayedItemStacks = new LinkedList<>();
|
||||
|
||||
@@ -54,7 +54,7 @@ public class TradingStationTargetSelectScreen extends Screen {
|
||||
protected TradingStationTargetSelectScreen(Component pTitle) {
|
||||
super(pTitle);
|
||||
}
|
||||
public TradingStationTargetSelectScreen(TradingStationBlockEntity pBlockEntity) {
|
||||
public TradingStationTargetSelectScreen(ITradingStationBlockEntity pBlockEntity) {
|
||||
this(ModLang.translate("select_target.title"));
|
||||
this.blockEntity = pBlockEntity;
|
||||
this.allPossibleOutputs = ModRecipes.getAllOutputs(pBlockEntity.getLevel());
|
||||
|
||||
@@ -4,30 +4,86 @@ import com.oierbravo.trading_station.content.trading_station.TradingStationBlock
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationBlockEntity;
|
||||
import com.oierbravo.trading_station.registrate.ModBlockEntities;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.InteractionResult;
|
||||
import net.minecraft.world.MenuProvider;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.context.BlockPlaceContext;
|
||||
import net.minecraft.world.level.BlockGetter;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.BaseEntityBlock;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.RenderShape;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.block.state.StateDefinition;
|
||||
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
||||
import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import net.minecraft.world.phys.shapes.CollisionContext;
|
||||
import net.minecraft.world.phys.shapes.Shapes;
|
||||
import net.minecraft.world.phys.shapes.VoxelShape;
|
||||
import net.minecraftforge.network.NetworkHooks;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import static net.minecraft.world.level.block.state.properties.BlockStateProperties.HORIZONTAL_FACING;
|
||||
|
||||
|
||||
public class PoweredTradingStationBlock extends TradingStationBlock {
|
||||
|
||||
|
||||
public PoweredTradingStationBlock(Properties pProperties) {
|
||||
super(pProperties);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
|
||||
return ModBlockEntities.POWERED_TRADING_STATION.create(pPos, pState);
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) {
|
||||
if(pLevel.isClientSide()) {
|
||||
return null;
|
||||
}
|
||||
return createTickerHelper(pBlockEntityType, ModBlockEntities.POWERED_TRADING_STATION.get(),
|
||||
TradingStationBlockEntity::tick);
|
||||
(pLevel1, pPos, pState1, pBlockEntity) -> pBlockEntity.tick(pLevel1, pPos, pState1));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState pNewState, boolean pIsMoving) {
|
||||
if (pState.getBlock() != pNewState.getBlock()) {
|
||||
BlockEntity blockEntity = pLevel.getBlockEntity(pPos);
|
||||
if (blockEntity instanceof PoweredTradingStationBlockEntity) {
|
||||
((PoweredTradingStationBlockEntity) blockEntity).drops();
|
||||
}
|
||||
}
|
||||
super.onRemove(pState, pLevel, pPos, pNewState, pIsMoving);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InteractionResult use(BlockState state, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand handIn, BlockHitResult hit) {
|
||||
if (!pLevel.isClientSide()) {
|
||||
BlockEntity blockEntity = pLevel.getBlockEntity(pPos);
|
||||
if(blockEntity instanceof PoweredTradingStationBlockEntity) {
|
||||
NetworkHooks.openScreen(((ServerPlayer)pPlayer), (PoweredTradingStationBlockEntity) blockEntity, pPos);
|
||||
} else {
|
||||
throw new IllegalStateException("Our Container provider is missing!");
|
||||
}
|
||||
}
|
||||
|
||||
return InteractionResult.sidedSuccess(pLevel.isClientSide());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,115 @@
|
||||
package com.oierbravo.trading_station.content.trading_station.powered;
|
||||
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationBlockEntity;
|
||||
import com.oierbravo.trading_station.foundation.util.ModEnergyStorage;
|
||||
import com.oierbravo.trading_station.foundation.util.ModLang;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.world.entity.player.Inventory;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ForgeCapabilities;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class PoweredTradingStationBlockEntity extends TradingStationBlockEntity {
|
||||
|
||||
private final ModEnergyStorage energyStorage = createEnergyStorage();
|
||||
private LazyOptional<IEnergyStorage> lazyEnergyHandler = LazyOptional.empty();
|
||||
|
||||
@Override
|
||||
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
|
||||
if(cap == ForgeCapabilities.ENERGY) {
|
||||
return lazyEnergyHandler.cast();
|
||||
}
|
||||
|
||||
return super.getCapability(cap, side);
|
||||
}
|
||||
|
||||
public PoweredTradingStationBlockEntity(BlockEntityType<?> pType, BlockPos pWorldPosition, BlockState pBlockState) {
|
||||
super(pType, pWorldPosition, pBlockState);
|
||||
}
|
||||
private ModEnergyStorage createEnergyStorage() {
|
||||
return new ModEnergyStorage(64000, 200) {
|
||||
@Override
|
||||
public void onEnergyChanged() {
|
||||
setChanged();
|
||||
getLevel().sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 3);
|
||||
}
|
||||
};
|
||||
}
|
||||
public IEnergyStorage getEnergyStorage() {
|
||||
return this.energyStorage;
|
||||
}
|
||||
public void setRemoved() {
|
||||
super.setRemoved();
|
||||
}
|
||||
public void onLoad() {
|
||||
super.onLoad();
|
||||
lazyEnergyHandler = LazyOptional.of(() -> energyStorage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidateCaps() {
|
||||
super.invalidateCaps();
|
||||
lazyEnergyHandler.invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void saveAdditional(@NotNull CompoundTag tag) {
|
||||
super.saveAdditional(tag);
|
||||
tag.putInt("energy", energyStorage.getEnergyStored());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(CompoundTag tag) {
|
||||
super.load(tag);
|
||||
energyStorage.setEnergy(tag.getInt("energy"));
|
||||
|
||||
}
|
||||
|
||||
public Component getDisplayName() {
|
||||
return ModLang.translate("powered_trading_station.block.display");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public AbstractContainerMenu createMenu(int pContainerId, Inventory pPlayerInventory, Player pPlayer) {
|
||||
return new PoweredTradingStationMenu(pContainerId, pPlayerInventory, this, this.containerData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick(Level pLevel, BlockPos pPos, BlockState pState) {
|
||||
super.tick(pLevel, pPos, pState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateProgress() {
|
||||
super.updateProgress();
|
||||
extractEnergy();
|
||||
}
|
||||
private void extractEnergy() {
|
||||
this.energyStorage.extractEnergy(1000, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean canCraftItem() {
|
||||
Level level = this.getLevel();
|
||||
if(level == null)
|
||||
return false;
|
||||
|
||||
if(this.energyStorage.getEnergyStored() < 1000){
|
||||
return false;
|
||||
}
|
||||
return super.canCraftItem();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.oierbravo.trading_station.content.trading_station.powered;
|
||||
|
||||
import com.oierbravo.trading_station.content.trading_station.ITradingStationBlockEntity;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationBlockEntity;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationMenu;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationScreen;
|
||||
import com.oierbravo.trading_station.foundation.gui.AbstractTradingMenu;
|
||||
import com.oierbravo.trading_station.registrate.ModBlocks;
|
||||
import com.oierbravo.trading_station.registrate.ModMenus;
|
||||
import net.minecraft.client.Minecraft;
|
||||
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.*;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraftforge.items.ItemStackHandler;
|
||||
import net.minecraftforge.items.SlotItemHandler;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class PoweredTradingStationMenu extends AbstractTradingMenu {
|
||||
public PoweredTradingStationMenu(int pContainerId, Inventory pInv, BlockEntity pBlockEntity, ContainerData pData) {
|
||||
super(ModMenus.POWERED_TRADING_STATION.get(), pContainerId, pInv, pBlockEntity, pData);
|
||||
}
|
||||
|
||||
public PoweredTradingStationMenu(int pContainerId, Inventory inventory, FriendlyByteBuf buf) {
|
||||
super(ModMenus.POWERED_TRADING_STATION.get(), pContainerId, inventory, buf);
|
||||
}
|
||||
|
||||
public static PoweredTradingStationMenu factory(@Nullable MenuType<PoweredTradingStationMenu> pMenuType, int pContainerId, Inventory inventory, FriendlyByteBuf buf) {
|
||||
return new PoweredTradingStationMenu(pContainerId, inventory, buf);
|
||||
}
|
||||
@Override
|
||||
public boolean stillValid(Player pPlayer) {
|
||||
return stillValid(ContainerLevelAccess.create(level, blockEntity.getBlockPos()), pPlayer, ModBlocks.POWERED_TRADING_STATION.get());
|
||||
}
|
||||
@Override
|
||||
public int[][] getInputSlotCoords() {
|
||||
return new int[][]{{28,38},{51,38}};
|
||||
}
|
||||
@Override
|
||||
public int[] getOutputSlotCoords() {
|
||||
return new int[]{131,38};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.oierbravo.trading_station.content.trading_station.powered;
|
||||
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import com.oierbravo.trading_station.TradingStation;
|
||||
import com.oierbravo.trading_station.content.trading_recipe.TradingRecipe;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationMenu;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationTargetSelectScreen;
|
||||
import com.oierbravo.trading_station.foundation.gui.AbstractTradingScreen;
|
||||
import com.oierbravo.trading_station.foundation.render.EnergyDisplayTooltipArea;
|
||||
import com.oierbravo.trading_station.foundation.render.FakeItemRenderer;
|
||||
import com.oierbravo.trading_station.foundation.util.ModLang;
|
||||
import com.oierbravo.trading_station.registrate.ModRecipes;
|
||||
import net.minecraft.client.Minecraft;
|
||||
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.item.crafting.Ingredient;
|
||||
import net.minecraftforge.client.gui.widget.ExtendedButton;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class PoweredTradingStationScreen extends AbstractTradingScreen<PoweredTradingStationMenu> {
|
||||
|
||||
protected static int[] progressArrowCoords = {79,47};
|
||||
protected static int[] targetSelectButtonCoords = {131,18};
|
||||
private static final ResourceLocation TEXTURE = TradingStation.asResource("textures/gui/powered_trading_station.png");
|
||||
private EnergyDisplayTooltipArea energyInfoArea;
|
||||
|
||||
|
||||
public PoweredTradingStationScreen(PoweredTradingStationMenu pMenu, Inventory pPlayerInventory, Component pTitle) {
|
||||
super(pMenu, pPlayerInventory, pTitle);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
super.init();
|
||||
assignEnergyInfoArea();
|
||||
}
|
||||
private void assignEnergyInfoArea() {
|
||||
//energyInfoArea = new EnergyDisplayTooltipArea(0,0, menu.blockEntity.getEnergyStorage());
|
||||
energyInfoArea = new EnergyDisplayTooltipArea(((width - imageWidth) / 2) + 8,((height - imageHeight) / 2) + 24, menu.blockEntity.getEnergyStorage());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void renderBg(PoseStack pPoseStack, float pPartialTick, int pMouseX, int pMouseY) {
|
||||
|
||||
super.renderBg(pPoseStack, pPartialTick, pMouseX, pMouseY);
|
||||
energyInfoArea.render(pPoseStack);
|
||||
|
||||
|
||||
}
|
||||
@Override
|
||||
protected void renderLabels(PoseStack pPoseStack, int pMouseX, int pMouseY) {
|
||||
int x = (width - imageWidth) / 2;
|
||||
int y = (height - imageHeight) / 2;
|
||||
|
||||
renderEnergyAreaTooltip(pPoseStack, pMouseX, pMouseY, x, y);
|
||||
super.renderLabels(pPoseStack, pMouseX, pMouseY);
|
||||
}
|
||||
private void renderEnergyAreaTooltip(PoseStack pPoseStack, int pMouseX, int pMouseY, int x, int y) {
|
||||
if(isMouseAboveArea(pMouseX, pMouseY, x, y, 8, 24, 9, 30)) {
|
||||
this.renderTooltip(pPoseStack, energyInfoArea.getTooltips(),
|
||||
Optional.empty(), pMouseX - x, pMouseY - y);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
protected ResourceLocation getTexture() {
|
||||
return TEXTURE;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected int[] getProgressArrowCoords() {
|
||||
return progressArrowCoords;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int[] getTargetSelectButtonCoords() {
|
||||
return targetSelectButtonCoords;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package com.oierbravo.trading_station.foundation.gui;
|
||||
|
||||
import com.oierbravo.trading_station.content.trading_station.ITradingStationBlockEntity;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationBlockEntity;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationScreen;
|
||||
import com.oierbravo.trading_station.registrate.ModBlocks;
|
||||
import com.oierbravo.trading_station.registrate.ModMenus;
|
||||
import net.minecraft.client.Minecraft;
|
||||
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.*;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraftforge.items.SlotItemHandler;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public abstract class AbstractTradingMenu extends AbstractContainerMenu {
|
||||
|
||||
public final ITradingStationBlockEntity blockEntity;
|
||||
public final Inventory inventory;
|
||||
protected final Level level;
|
||||
private final ContainerData containerData;
|
||||
|
||||
|
||||
public AbstractTradingMenu(@Nullable MenuType<?> pMenuType, int pContainerId, Inventory pInv, BlockEntity pBlockEntity, ContainerData pData){
|
||||
super(pMenuType, pContainerId);
|
||||
checkContainerSize(pInv, 3);
|
||||
blockEntity = (ITradingStationBlockEntity) pBlockEntity;
|
||||
level = pInv.player.getLevel();
|
||||
containerData = pData;
|
||||
inventory = pInv;
|
||||
addPlayerHotbar(pInv);
|
||||
addPlayerInventory(pInv);
|
||||
this.addDataSlots(pData);
|
||||
//this.blockEntity.inputItems
|
||||
int[][] coords = getInputSlotCoords();
|
||||
|
||||
this.blockEntity.getInputItemHandler().ifPresent(itemHandler -> {
|
||||
this.addSlot(new SlotItemHandler(itemHandler,0, getInputSlotCoords()[0][0],getInputSlotCoords()[0][1]));
|
||||
this.addSlot(new SlotItemHandler(itemHandler,1, getInputSlotCoords()[1][0],getInputSlotCoords()[1][1]));
|
||||
});
|
||||
this.blockEntity.getOutputItemHandler().ifPresent(itemHandler -> {
|
||||
this.addSlot(new SlotItemHandler(itemHandler,0,getOutputSlotCoords()[0],getOutputSlotCoords()[1]));
|
||||
});
|
||||
this.addSlot(new SlotItemHandler(this.blockEntity.getTargetItemHandler(),0,87,28));
|
||||
|
||||
|
||||
}
|
||||
|
||||
public AbstractTradingMenu(@Nullable MenuType<?> pMenuType, int pContainerId, Inventory inv, FriendlyByteBuf extraData){
|
||||
this(pMenuType, pContainerId, inv, inv.player.level.getBlockEntity(extraData.readBlockPos()), new SimpleContainerData(2));
|
||||
}
|
||||
public abstract int[][] getInputSlotCoords();
|
||||
|
||||
public abstract int[] getOutputSlotCoords();
|
||||
|
||||
@Override
|
||||
public abstract boolean stillValid(Player pPlayer);
|
||||
|
||||
public int getScaledProgress() {
|
||||
int progress = this.containerData.get(0);
|
||||
int maxProgress = this.containerData.get(1); // Max Progress
|
||||
int progressArrowSize = 34; // This is the height in pixels of your arrow
|
||||
|
||||
return maxProgress != 0 && progress != 0 ? progress * progressArrowSize / maxProgress : 0;
|
||||
}
|
||||
public boolean isCrafting() {
|
||||
return containerData.get(0) > 0;
|
||||
}
|
||||
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 = 2; // must be the number of slots you have!
|
||||
@Override
|
||||
public ItemStack quickMoveStack(Player playerIn, int pIndex) {
|
||||
Slot sourceSlot = slots.get(pIndex);
|
||||
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 (pIndex < 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 (pIndex < 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 if (pIndex == TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT) {
|
||||
// This is a TE Outpu 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:" + pIndex);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
private static final int PLAYER_INVENTORY_Y = 64;
|
||||
|
||||
private void addPlayerInventory(Inventory playerInventory) {
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int l = 0; l < 9; ++l) {
|
||||
this.addSlot(new Slot(playerInventory, l + i * 9 + 9, 8 + l * 18, PLAYER_INVENTORY_Y + i * 18));
|
||||
}
|
||||
}
|
||||
}
|
||||
private static final int HOTBAR_Y = 122;
|
||||
|
||||
private void addPlayerHotbar(Inventory playerInventory) {
|
||||
for (int i = 0; i < 9; ++i) {
|
||||
this.addSlot(new Slot(playerInventory, i, 8 + i * 18, HOTBAR_Y));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.oierbravo.trading_station.foundation.gui;
|
||||
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import com.oierbravo.trading_station.TradingStation;
|
||||
import com.oierbravo.trading_station.content.trading_recipe.TradingRecipe;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationScreen;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationTargetSelectScreen;
|
||||
import com.oierbravo.trading_station.foundation.render.FakeItemRenderer;
|
||||
import com.oierbravo.trading_station.foundation.util.ModLang;
|
||||
import com.oierbravo.trading_station.foundation.util.MouseUtil;
|
||||
import com.oierbravo.trading_station.registrate.ModRecipes;
|
||||
import net.minecraft.client.Minecraft;
|
||||
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.item.crafting.Ingredient;
|
||||
import net.minecraftforge.client.gui.widget.ExtendedButton;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class AbstractTradingScreen<MENU extends AbstractTradingMenu> extends AbstractContainerScreen<MENU> {
|
||||
|
||||
public AbstractTradingScreen(MENU pMenu, Inventory pPlayerInventory, Component pTitle) {
|
||||
super(pMenu, pPlayerInventory, pTitle);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
super.init();
|
||||
this.titleLabelX = 4;
|
||||
this.titleLabelY = 4;
|
||||
this.inventoryLabelY = 100000;
|
||||
this.addRenderableWidget(new ExtendedButton(leftPos + getTargetSelectButtonCoords()[0],topPos + getTargetSelectButtonCoords()[1],16,16, ModLang.translate("select_target.button"), btn ->{
|
||||
TradingStationTargetSelectScreen screen = new TradingStationTargetSelectScreen( this.menu.blockEntity);
|
||||
Minecraft.getInstance().pushGuiLayer(screen);
|
||||
|
||||
}));
|
||||
}
|
||||
@Override
|
||||
protected void renderBg(PoseStack pPoseStack, float pPartialTick, int pMouseX, int pMouseY) {
|
||||
RenderSystem.setShader(GameRenderer::getPositionTexShader);
|
||||
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
RenderSystem.setShaderTexture(0, getTexture());
|
||||
this.fillGradient(pPoseStack, 0, 0, this.width, this.height, -1072689136, -804253680);
|
||||
|
||||
int x = (width - imageWidth) / 2;
|
||||
int y = (height - imageHeight) / 2;
|
||||
this.blit(pPoseStack, x, y, 0, 0, imageWidth, imageHeight);
|
||||
|
||||
if (menu.isCrafting()) {
|
||||
this.blit(pPoseStack, x + getProgressArrowCoords()[0], y + getProgressArrowCoords()[1], 179, 0, menu.getScaledProgress(), 7);
|
||||
}
|
||||
|
||||
if(!menu.blockEntity.getTargetItemStack().isEmpty()){
|
||||
Optional<TradingRecipe> recipe = ModRecipes.findByOutput(menu.blockEntity.getLevel(),menu.blockEntity.getTargetItemStack());
|
||||
if(recipe.isPresent()){
|
||||
renderFakeRecipe(recipe.get(), pPoseStack);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void renderFakeRecipe(TradingRecipe recipe, PoseStack pPoseStack){
|
||||
for(int index = 0; index < recipe.getIngredients().size(); index++){
|
||||
Ingredient ingredient = recipe.getIngredients().get(index);
|
||||
int[][] coords = this.menu.getInputSlotCoords();
|
||||
|
||||
if(!ingredient.isEmpty())
|
||||
FakeItemRenderer.renderFakeItem(ingredient.getItems()[0],getGuiLeft() + this.menu.getInputSlotCoords()[index][0],getGuiTop() + this.menu.getInputSlotCoords()[index][1], .5f);
|
||||
}
|
||||
FakeItemRenderer.renderFakeItem(recipe.getResultItem(),getGuiLeft() + this.menu.getOutputSlotCoords()[0],getGuiTop() + this.menu.getOutputSlotCoords()[1], .5f);
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected abstract int[] getProgressArrowCoords();
|
||||
|
||||
protected abstract int[] getTargetSelectButtonCoords();
|
||||
|
||||
|
||||
@Override
|
||||
public void render(PoseStack pPoseStack, int pMouseX, int pMouseY, float pPartialTick) {
|
||||
super.render(pPoseStack, pMouseX, pMouseY, pPartialTick);
|
||||
}
|
||||
|
||||
protected abstract ResourceLocation getTexture();
|
||||
|
||||
protected boolean isMouseAboveArea(int pMouseX, int pMouseY, int x, int y, int offsetX, int offsetY, int width, int height) {
|
||||
return MouseUtil.isMouseOver(pMouseX, pMouseY, x + offsetX, y + offsetY, width, height);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.oierbravo.trading_station.foundation.render;
|
||||
|
||||
import com.mojang.blaze3d.vertex.PoseStack;
|
||||
import net.minecraft.client.gui.GuiComponent;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
|
||||
import java.util.List;
|
||||
/*
|
||||
* BluSunrize
|
||||
* Copyright (c) 2021
|
||||
*
|
||||
* This code is licensed under "Blu's License of Common Sense"
|
||||
* https://github.com/BluSunrize/ImmersiveEngineering/blob/1.19.2/LICENSE
|
||||
*
|
||||
* Slightly Modified Version by: oierbravo
|
||||
*/
|
||||
public class EnergyDisplayTooltipArea extends GuiComponent {
|
||||
private final int xPos;
|
||||
private final int yPos;
|
||||
private final int width;
|
||||
private final int height;
|
||||
private final IEnergyStorage energy;
|
||||
|
||||
public EnergyDisplayTooltipArea(int xMin, int yMin, IEnergyStorage energy) {
|
||||
this(xMin, yMin, energy,9,30);
|
||||
}
|
||||
|
||||
public EnergyDisplayTooltipArea(int xMin, int yMin, IEnergyStorage energy, int width, int height) {
|
||||
xPos = xMin;
|
||||
yPos = yMin;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.energy = energy;
|
||||
}
|
||||
|
||||
public List<Component> getTooltips() {
|
||||
return List.of(Component.literal(energy.getEnergyStored()+" / "+energy.getMaxEnergyStored()+" FE"));
|
||||
}
|
||||
|
||||
public void render(PoseStack pPoseStack) {
|
||||
int stored = (int)(height * (energy.getEnergyStored() / (float)energy.getMaxEnergyStored()));
|
||||
fillGradient(pPoseStack,xPos,yPos + (height - stored),xPos + width, yPos + height,0xffb51500, 0xff600b00);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.oierbravo.trading_station.content.trading_station;
|
||||
package com.oierbravo.trading_station.foundation.render;
|
||||
|
||||
import com.mojang.blaze3d.platform.GlStateManager;
|
||||
import com.mojang.blaze3d.platform.Lighting;
|
||||
@@ -16,6 +16,14 @@ import net.minecraft.client.renderer.texture.TextureManager;
|
||||
import net.minecraft.client.resources.model.BakedModel;
|
||||
import net.minecraft.world.inventory.InventoryMenu;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
/*
|
||||
* smashingmods
|
||||
*
|
||||
* This code is licensed under "GNU Lesser General Public License"
|
||||
* https://github.com/SmashingMods/AlchemyLib/blob/1.19.x/src/main/java/com/smashingmods/alchemylib/api/blockentity/container/FakeItemRenderer.java
|
||||
*
|
||||
* Slightly Modified Version by: oierbravo
|
||||
*/
|
||||
|
||||
/**
|
||||
* This class can be used to render a 2D ItemStack to the screen with optional transparency. The item being rendered
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.oierbravo.trading_station.foundation.util;
|
||||
|
||||
import net.minecraftforge.energy.EnergyStorage;
|
||||
|
||||
public abstract class ModEnergyStorage extends EnergyStorage {
|
||||
public ModEnergyStorage(int capacity, int maxTransfer) {
|
||||
super(capacity, maxTransfer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int extractEnergy(int maxExtract, boolean simulate) {
|
||||
int extractedEnergy = super.extractEnergy(maxExtract, simulate);
|
||||
if(extractedEnergy != 0) {
|
||||
onEnergyChanged();
|
||||
}
|
||||
|
||||
return extractedEnergy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int receiveEnergy(int maxReceive, boolean simulate) {
|
||||
int receiveEnergy = super.receiveEnergy(maxReceive, simulate);
|
||||
if(receiveEnergy != 0) {
|
||||
onEnergyChanged();
|
||||
}
|
||||
|
||||
return receiveEnergy;
|
||||
}
|
||||
|
||||
public int setEnergy(int energy) {
|
||||
this.energy = energy;
|
||||
return energy;
|
||||
}
|
||||
|
||||
public abstract void onEnergyChanged();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.oierbravo.trading_station.foundation.util;
|
||||
|
||||
public class MouseUtil {
|
||||
public static boolean isMouseOver(double mouseX, double mouseY, int x, int y) {
|
||||
return isMouseOver(mouseX, mouseY, x, y, 16);
|
||||
}
|
||||
|
||||
public static boolean isMouseOver(double mouseX, double mouseY, int x, int y, int size) {
|
||||
return isMouseOver(mouseX, mouseY, x, y, size, size);
|
||||
}
|
||||
|
||||
public static boolean isMouseOver(double mouseX, double mouseY, int x, int y, int sizeX, int sizeY) {
|
||||
return (mouseX >= x && mouseX <= x + sizeX) && (mouseY >= y && mouseY <= y + sizeY);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.oierbravo.trading_station.network.packets;
|
||||
|
||||
import com.oierbravo.trading_station.content.trading_station.ITradingStationBlockEntity;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationBlockEntity;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.core.BlockPos;
|
||||
@@ -45,7 +46,7 @@ public class GhostItemSyncC2SPacket {
|
||||
if (container == null)
|
||||
return;
|
||||
|
||||
if(sender.getLevel().getBlockEntity(message.pos) instanceof TradingStationBlockEntity blockEntity) {
|
||||
if(sender.getLevel().getBlockEntity(message.pos) instanceof ITradingStationBlockEntity blockEntity) {
|
||||
blockEntity.setPreferedItem(message.itemStack);
|
||||
//blockEntity.setChanged();
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.oierbravo.trading_station.network.packets;
|
||||
|
||||
import com.oierbravo.trading_station.content.trading_station.ITradingStationBlockEntity;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationBlockEntity;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.core.BlockPos;
|
||||
@@ -44,7 +45,7 @@ public class ItemStackSyncS2CPacket {
|
||||
public static boolean handle(ItemStackSyncS2CPacket message, Supplier<NetworkEvent.Context> supplier) {
|
||||
NetworkEvent.Context context = supplier.get();
|
||||
context.enqueueWork(() -> {
|
||||
if(Minecraft.getInstance().level.getBlockEntity(message.pos) instanceof TradingStationBlockEntity blockEntity) {
|
||||
if(Minecraft.getInstance().level.getBlockEntity(message.pos) instanceof ITradingStationBlockEntity blockEntity) {
|
||||
blockEntity.setItemStack(message.slot,message.itemStack,message.slotType);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
package com.oierbravo.trading_station.network.packets;
|
||||
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationBlockEntity;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationMenu;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.SimpleMenuProvider;
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
import net.minecraftforge.network.NetworkHooks;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class OpenTargetSelectPacket {
|
||||
private BlockPos sourcePos;
|
||||
|
||||
public OpenTargetSelectPacket(BlockPos pSourcePos){
|
||||
this.sourcePos = pSourcePos;
|
||||
}
|
||||
public OpenTargetSelectPacket(FriendlyByteBuf buf){
|
||||
this(buf.readBlockPos());
|
||||
}
|
||||
|
||||
public void toBytes(FriendlyByteBuf buf) {
|
||||
buf.writeBlockPos(sourcePos);
|
||||
}
|
||||
public static boolean handle(OpenTargetSelectPacket message, Supplier<NetworkEvent.Context> supplier) {
|
||||
NetworkEvent.Context context = supplier.get();
|
||||
context.enqueueWork(() -> {
|
||||
ServerPlayer sender = context.getSender();
|
||||
if (sender == null)
|
||||
return;
|
||||
|
||||
TradingStationMenu container = (TradingStationMenu) sender.containerMenu;
|
||||
if (container == null)
|
||||
return;
|
||||
|
||||
/*NetworkHooks.openScreen(sender, new SimpleMenuProvider(
|
||||
(windowId, playerInventory, playerEntity) -> new TradingTradeSelectMenu(windowId, message.sourcePos), Component.translatable("")), (buf -> {
|
||||
buf.writeBlockPos(message.sourcePos);
|
||||
}));*/
|
||||
NetworkHooks.openScreen(((ServerPlayer)sender), (TradingStationBlockEntity)container.blockEntity, message.sourcePos);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package com.oierbravo.trading_station.network.packets;
|
||||
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationMenu;
|
||||
import dev.latvian.mods.rhino.ast.Block;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.SimpleMenuProvider;
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
import net.minecraftforge.network.NetworkHooks;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class OpenTradingMenuPacket {
|
||||
protected BlockPos sourcePos;
|
||||
|
||||
public OpenTradingMenuPacket(BlockPos pSourcePos){
|
||||
this.sourcePos = pSourcePos;
|
||||
}
|
||||
public OpenTradingMenuPacket(FriendlyByteBuf buf){
|
||||
this(buf.readBlockPos());
|
||||
}
|
||||
|
||||
public void toBytes(FriendlyByteBuf buf) {
|
||||
buf.writeBlockPos(sourcePos);
|
||||
}
|
||||
|
||||
public BlockPos getSourcePos(){
|
||||
return sourcePos;
|
||||
}
|
||||
public static boolean handle(OpenTradingMenuPacket message, Supplier<NetworkEvent.Context> supplier) {
|
||||
NetworkEvent.Context context = supplier.get();
|
||||
context.enqueueWork(() -> {
|
||||
ServerPlayer sender = context.getSender();
|
||||
if (sender == null)
|
||||
return;
|
||||
|
||||
AbstractContainerMenu container = sender.containerMenu;
|
||||
if (container == null)
|
||||
return;
|
||||
|
||||
NetworkHooks.openScreen(sender, new SimpleMenuProvider(
|
||||
(windowId, playerInventory, playerEntity) -> new TradingStationMenu(windowId,playerInventory,message.sourcePos), Component.translatable("trading_station.block.display")), (buf -> {
|
||||
buf.writeBlockPos(message.sourcePos);
|
||||
}));
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,22 +3,18 @@ package com.oierbravo.trading_station.registrate;
|
||||
import com.oierbravo.trading_station.TradingStation;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationMenu;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingStationScreen;
|
||||
import com.oierbravo.trading_station.content.trading_station.powered.PoweredTradingStationMenu;
|
||||
import com.oierbravo.trading_station.content.trading_station.powered.PoweredTradingStationScreen;
|
||||
import com.tterrag.registrate.util.entry.MenuEntry;
|
||||
import com.tterrag.registrate.util.entry.RegistryEntry;
|
||||
import net.minecraft.client.gui.screens.inventory.ContainerScreen;
|
||||
|
||||
import net.minecraft.world.SimpleContainer;
|
||||
import net.minecraft.world.inventory.ChestMenu;
|
||||
import net.minecraft.world.inventory.MenuType;
|
||||
|
||||
public class ModMenus {
|
||||
public static final MenuEntry<TradingStationMenu> TRADING_STATION = TradingStation.registrate()
|
||||
.menu("trading_station",TradingStationMenu::factory, () -> TradingStationScreen::new)
|
||||
.register();
|
||||
|
||||
public static final MenuEntry<TradingStationMenu> TRADING_STATION_TRADE_SELECT = TradingStation.registrate()
|
||||
.menu("trading_station_trade_select",TradingStationMenu::factory, () -> TradingStationScreen::new)
|
||||
public static final MenuEntry<PoweredTradingStationMenu> POWERED_TRADING_STATION = TradingStation.registrate()
|
||||
.menu("powered_trading_station", PoweredTradingStationMenu::factory, () -> PoweredTradingStationScreen::new)
|
||||
.register();
|
||||
|
||||
public static void register() {
|
||||
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@ package com.oierbravo.trading_station.registrate;
|
||||
import com.oierbravo.trading_station.TradingStation;
|
||||
import com.oierbravo.trading_station.network.packets.GhostItemSyncC2SPacket;
|
||||
import com.oierbravo.trading_station.network.packets.ItemStackSyncS2CPacket;
|
||||
import com.oierbravo.trading_station.network.packets.OpenTargetSelectPacket;
|
||||
import com.oierbravo.trading_station.network.packets.OpenTradingMenuPacket;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraftforge.network.NetworkDirection;
|
||||
@@ -43,17 +41,6 @@ public class ModMessages {
|
||||
.consumerMainThread(GhostItemSyncC2SPacket::handle)
|
||||
.add();
|
||||
|
||||
net.messageBuilder(OpenTradingMenuPacket.class, id(), NetworkDirection.PLAY_TO_SERVER)
|
||||
.decoder(OpenTradingMenuPacket::new)
|
||||
.encoder(OpenTradingMenuPacket::toBytes)
|
||||
.consumerMainThread(OpenTradingMenuPacket::handle)
|
||||
.add();
|
||||
|
||||
net.messageBuilder(OpenTargetSelectPacket.class, id(), NetworkDirection.PLAY_TO_SERVER)
|
||||
.decoder(OpenTargetSelectPacket::new)
|
||||
.encoder(OpenTargetSelectPacket::toBytes)
|
||||
.consumerMainThread(OpenTargetSelectPacket::handle)
|
||||
.add();
|
||||
}
|
||||
|
||||
public static <MSG> void sendToServer(MSG message) {
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package com.oierbravo.trading_station.registrate;
|
||||
|
||||
import com.oierbravo.trading_station.TradingStation;
|
||||
import com.oierbravo.trading_station.content.trading_station.TradingRecipe;
|
||||
import com.oierbravo.trading_station.content.trading_recipe.TradingRecipe;
|
||||
import net.minecraft.world.SimpleContainer;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.RecipeSerializer;
|
||||
import net.minecraft.world.item.crafting.RecipeType;
|
||||
@@ -13,11 +12,8 @@ import net.minecraftforge.registries.DeferredRegister;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
import net.minecraftforge.registries.RegistryObject;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ModRecipes {
|
||||
public static final DeferredRegister<RecipeSerializer<?>> SERIALIZERS =
|
||||
@@ -35,33 +31,16 @@ public class ModRecipes {
|
||||
return Optional.empty();
|
||||
return pLevel.getRecipeManager().getRecipeFor(TradingRecipe.Type.INSTANCE,pInv,pLevel);
|
||||
}
|
||||
/*public static List<ItemStack> getAllOutputs(Level pLevel){
|
||||
List<ItemStack> allOutputs = pLevel.getRecipeManager().getAllRecipesFor(TradingRecipe.Type.INSTANCE).stream()
|
||||
.map(TradingRecipe::getResult).toList();
|
||||
allOutputs.add(ItemStack.EMPTY);
|
||||
return allOutputs;
|
||||
|
||||
}*/
|
||||
public static List<ItemStack> getAllOutputs(Level pLevel){
|
||||
return pLevel.getRecipeManager().getAllRecipesFor(TradingRecipe.Type.INSTANCE).stream()
|
||||
.map(TradingRecipe::getResult).toList();
|
||||
|
||||
}
|
||||
public static Optional<TradingRecipe> findByOutput(Level pLevel,ItemStack targetedOutput){
|
||||
//if(pLevel.isClientSide())
|
||||
// return Optional.empty();
|
||||
return pLevel.getRecipeManager().getAllRecipesFor(TradingRecipe.Type.INSTANCE).stream()
|
||||
.filter(recipe -> recipe.matchesOutput(targetedOutput)).findFirst();
|
||||
|
||||
}
|
||||
/*public static Optional<TradingRecipe> findWithPreferdOutput(SimpleContainer pInv, Level pLevel, ItemStack preferedOutput){
|
||||
if(pLevel.isClientSide())
|
||||
return Optional.empty();
|
||||
List<TradingRecipe> allTradingRecipes = pLevel.getRecipeManager().getAllRecipesFor(TradingRecipe.Type.INSTANCE);
|
||||
|
||||
return allTradingRecipes.stream().filter(extrudingRecipe -> TradingRecipe.matches(tradingStation,tradingRecipe)).findFirst();
|
||||
|
||||
}*/
|
||||
public static void register(IEventBus eventBus) {
|
||||
SERIALIZERS.register(eventBus);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user