Skip to content

Commit

Permalink
Changed Packets
Browse files Browse the repository at this point in the history
Signed-off-by: Type-32 <[email protected]>
  • Loading branch information
Type-32 committed Jun 11, 2024
1 parent 1053a92 commit 36c098c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 77 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ create_version = 0.5.1.f-332
flywheel_version = 0.6.10-105
registrate_version = MC1.18.2-1.1.3

tacz_dependency = tacz-1.0.0-hotfix6-1.18.2
tacz_dependency = tacz-1.18.2-1.0.0-beta

jei_version=10.2.1.1006
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ public static void tick(Level pLevel, BlockPos pPos, BlockState pState, Decompon
pBlockEntity.craftItem(pBlockEntity);
pBlockEntity.setProcessingTime(0);
pBlockEntity.setSelectedRecipeIndex(-1);
pBlockEntity.startDecomponentalizationProcess(-1);
pBlockEntity.setCurrentRecipeIndex(-1);
setChanged(pLevel, pPos, pState);
}
} else {
pBlockEntity.setProcessingTime(0);
pBlockEntity.setSelectedRecipeIndex(-1);
pBlockEntity.startDecomponentalizationProcess(-1);
pBlockEntity.setCurrentRecipeIndex(-1);
setChanged(pLevel, pPos, pState);
}
}
Expand Down Expand Up @@ -188,7 +188,6 @@ private void craftItem(DecomponentalizerBlockEntity pBlockEntity) {
if (pBlockEntity.getCurrentRecipeIndex() != -1 && canOutput(pBlockEntity.getCurrentRecipe().getResultItem())) {
itemHandler.extractItem(0, 1, false);
itemHandler.extractItem(1, 1, false);
// itemHandler.extractItem(2, 1, false);
itemHandler.setStackInSlot(3, new ItemStack(getCurrentRecipe().getResultItem().getItem(), itemHandler.getStackInSlot(3).getCount() + 1));
}
}
Expand Down Expand Up @@ -241,8 +240,8 @@ public void setCurrentRecipeIndex(int availableRecipeIndex, List<Decomponentaliz
this.currentRecipeIndex = availableRecipeIndex >= availableRecipes.size() || availableRecipeIndex < 0 ? -1 : availableRecipeIndex;
}

public void startDecomponentalizationProcess(int availableRecipeIndex) {
List<DecomponentalizingRecipe> availableRecipes = getAvailableRecipes();
public void startDecomponentalizationProcess(ItemStack stack, int availableRecipeIndex) {
List<DecomponentalizingRecipe> availableRecipes = getAvailableRecipes(stack);
setCurrentRecipeIndex(availableRecipeIndex, availableRecipes);
if(availableRecipeIndex != -1 && availableRecipeIndex < availableRecipes.size() && getCurrentRecipeIndex() > -1) {
setTotalProcessingTime(availableRecipes.get(availableRecipeIndex).getProcessingTime());
Expand Down Expand Up @@ -275,6 +274,22 @@ public List<DecomponentalizingRecipe> getAvailableRecipes() {
return level.getRecipeManager().getRecipesFor(DecomponentalizingRecipeType.INSTANCE, new SimpleContainer(componentStack), level);
}

public List<DecomponentalizingRecipe> getAvailableRecipes(ItemStack stack) {
Level level = this.level;
if (level == null) {
Main.LOGGER.info("level is Empty");
return Collections.emptyList();
}

ItemStack componentStack = stack;
if (componentStack.isEmpty()) {
Main.LOGGER.info("componentStack is Empty");
return Collections.emptyList();
}

return level.getRecipeManager().getRecipesFor(DecomponentalizingRecipeType.INSTANCE, new SimpleContainer(componentStack), level);
}

public void setSelectedRecipeIndex(int availableRecipeIndex) {
List<DecomponentalizingRecipe> availableRecipes = getAvailableRecipes();
this.selectedRecipeIndex = availableRecipeIndex >= availableRecipes.size() || availableRecipeIndex < 0 ? -1 : availableRecipeIndex;
Expand All @@ -298,38 +313,4 @@ public DecomponentalizingRecipe getCurrentRecipe(){
public DecomponentalizingRecipe getSelectedRecipe(){
return getAvailableRecipeFromIndex(selectedRecipeIndex);
}

/**
* Sets the current worked-on recipe
* @param recipe The recipe in the list of available recipes
* @return Returns true if success and that the given recipe is in the list of available recipes, false if otherwise
*/
public boolean setCurrentRecipe(DecomponentalizingRecipe recipe) {
List<DecomponentalizingRecipe> availableRecipes = getAvailableRecipes();

if(availableRecipes.contains(recipe)){
currentRecipeIndex = availableRecipes.indexOf(recipe);
return true;
}else{
currentRecipeIndex = -1;
return false;
}
}

/**
* Sets the selected recipe
* @param recipe The recipe in the list of available recipes
* @return Returns true if success and that the given recipe is in the list of available recipes, false if otherwise
*/
public boolean setSelectedRecipe(DecomponentalizingRecipe recipe) {
List<DecomponentalizingRecipe> availableRecipes = getAvailableRecipes();

if(availableRecipes.contains(recipe)){
selectedRecipeIndex = availableRecipes.indexOf(recipe);
return true;
}else{
selectedRecipeIndex = -1;
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class DecomponentalizerContainerMenu extends AbstractContainerMenu {
private final DecomponentalizerBlockEntity blockEntity;
private final ContainerData data;
private final Level level;
private List<DecomponentalizingRecipe> availableRecipes = new ArrayList<DecomponentalizingRecipe>();

public DecomponentalizerContainerMenu(int id, Inventory inventory, FriendlyByteBuf extraData){
this(id, inventory, inventory.player.level.getBlockEntity(extraData.readBlockPos()), new SimpleContainerData(3));
Expand Down Expand Up @@ -149,16 +148,7 @@ public ItemStack quickMoveStack(Player playerIn, int index) {
public void startRecipeProcess() {
Main.LOGGER.debug("Decomponentalizing Selected Recipe is null? {}", blockEntity.getSelectedRecipe() == null);
if (!isCrafting() && blockEntity.getSelectedRecipe() != null) {
// blockEntity.setProcessing(1);
// setCurrentRecipe(blockEntity.getSelectedRecipe());
// this.data.set(0, 0);
// this.data.set(1, blockEntity.getCurrentRecipe().getProcessingTime());
// lockInputSlots();
// lockAnalyzeButton();
// blockEntity.setChanged();

// PacketHandler.sendToServer(new C2SSetDecomponentalizerSelectedRecipePacket(blockEntity.getBlockPos(), (byte) blockEntity.getSelectedRecipeIndex()));
PacketHandler.sendToServer(new C2SSetDecomponentalizerCurrentRecipePacket(blockEntity.getBlockPos(), (byte) blockEntity.getCurrentRecipeIndex()));
PacketHandler.sendToServer(new C2SSetDecomponentalizerCurrentRecipePacket(blockEntity.getBlockPos(), this.getBlockEntity().getItemHandler().getStackInSlot(2), (short) blockEntity.getCurrentRecipeIndex()));

Main.LOGGER.debug("Starting Decomponentalizing Process");
}
Expand Down Expand Up @@ -194,16 +184,6 @@ private void unlockAnalyzeButton() {
// You'll need to implement this based on your GUI setup
}

public void setSelectedRecipe(DecomponentalizingRecipe recipe) {
blockEntity.setSelectedRecipe(recipe);
// blockEntity.setChanged();
}

public void setCurrentRecipe(DecomponentalizingRecipe recipe) {
blockEntity.setCurrentRecipe(recipe);
blockEntity.setChanged();
}

public DecomponentalizerBlockEntity getBlockEntity() {
return blockEntity;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,56 @@
package cn.crtlprototypestudios.precisemanufacturing.foundation.network.packets;

import cn.crtlprototypestudios.precisemanufacturing.Main;
import cn.crtlprototypestudios.precisemanufacturing.foundation.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.network.NetworkEvent;

import java.util.Objects;
import java.util.function.Supplier;

public class C2SSetDecomponentalizerCurrentRecipePacket {
private final BlockPos position;
private final byte recipeIndex;
private final short recipeIndex;
private final ItemStack stack;

public C2SSetDecomponentalizerCurrentRecipePacket(BlockPos position, byte recipeIndex) {
public C2SSetDecomponentalizerCurrentRecipePacket(BlockPos position, ItemStack stack, short recipeIndex) {
this.position = position;
this.recipeIndex = recipeIndex;
this.stack = stack;
}

public C2SSetDecomponentalizerCurrentRecipePacket(FriendlyByteBuf buf) {
this(buf.readBlockPos(), buf.readByte());
this(buf.readBlockPos(), buf.readItem(), buf.readByte());
}

public void encode(FriendlyByteBuf buf) {
buf.writeBlockPos(position);
buf.writeItem(stack);
buf.writeByte(recipeIndex);
}

public static void handle(C2SSetDecomponentalizerCurrentRecipePacket msg, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
// DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> C2SSetDecomponentalizerCurrentRecipePacket.handleOnClient(msg, ctx));
// DistExecutor.unsafeRunWhenOn(Dist.DEDICATED_SERVER, () -> () -> C2SSetDecomponentalizerCurrentRecipePacket.handleOnServer(msg, ctx));
handleOnServer(msg, ctx);
});

ctx.get().setPacketHandled(true);
}

public static void handleOnClient(C2SSetDecomponentalizerCurrentRecipePacket msg, Supplier<NetworkEvent.Context> ctx){

}

public static void handleOnServer(C2SSetDecomponentalizerCurrentRecipePacket msg, Supplier<NetworkEvent.Context> ctx){
ServerPlayer player = ctx.get().getSender();
assert player != null;
ServerLevel world = player.getLevel();
assert world.hasChunkAt(msg.position);
world.getBlockEntity(msg.position, ModBlockEntities.DECOMPONENTALIZER.get()).get().startDecomponentalizationProcess(msg.recipeIndex);
try {
Objects.requireNonNull(world.getBlockEntity(msg.position, ModBlockEntities.DECOMPONENTALIZER.get()).orElse(null)).startDecomponentalizationProcess(msg.stack, msg.recipeIndex);
} catch(Exception e) {
Main.LOGGER.error("Error occurred receiving packet: ", e);
}
}


Expand Down
12 changes: 6 additions & 6 deletions src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ description='''${mod_description}'''
versionRange="[0.5.1.f,)"
ordering="NONE"
side="BOTH"
#[[dependencies."${mod_id}"]]
# modId="tacz"
# mandatory=true
# versionRange="*"
# ordering="AFTER"
# side="BOTH"
[[dependencies."${mod_id}"]]
modId="tacz"
mandatory=true
versionRange="[1.0.0,)"
ordering="NONE"
side="BOTH"

0 comments on commit 36c098c

Please sign in to comment.