Skip to content

Commit

Permalink
much cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuzss committed Feb 10, 2023
1 parent 63729db commit 9d3d1e4
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 159 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog].

## [v4.2.2-1.19.2] - 2023-02-10
### Changed
- Minor refactors and clean-ups
### Fixed
- Fixed empty item stacks being serialized occasionally leading to huge nbt tags
- Fixed rendering calculations running on the server, too, which is unnecessary

## [v4.2.1-1.19.2] - 2023-01-06
### Fixed
- Fixed crafting tables ignoring the player sneaking when clicked upon (the menu would open regardless and prevent placing any blocks against the crafting table)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package fuzs.visualworkbench.client;

import fuzs.puzzleslib.client.core.ClientModConstructor;
import fuzs.visualworkbench.client.renderer.blockentity.WorkbenchTileEntityRenderer;
import fuzs.visualworkbench.client.renderer.blockentity.WorkbenchBlockEntityRenderer;
import fuzs.visualworkbench.init.ModRegistry;
import net.minecraft.client.gui.screens.inventory.CraftingScreen;

public class VisualWorkbenchClient implements ClientModConstructor {

@Override
public void onRegisterBlockEntityRenderers(BlockEntityRenderersContext context) {
context.registerBlockEntityRenderer(ModRegistry.CRAFTING_TABLE_BLOCK_ENTITY.get(), WorkbenchTileEntityRenderer::new);
context.registerBlockEntityRenderer(ModRegistry.CRAFTING_TABLE_BLOCK_ENTITY.get(), WorkbenchBlockEntityRenderer::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.core.Registry;
import net.minecraft.util.Mth;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

public class WorkbenchTileEntityRenderer implements BlockEntityRenderer<CraftingTableBlockEntity> {
private final ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer();

public WorkbenchTileEntityRenderer(BlockEntityRendererProvider.Context context) {
public class WorkbenchBlockEntityRenderer implements BlockEntityRenderer<CraftingTableBlockEntity> {
private final ItemRenderer itemRenderer;

public WorkbenchBlockEntityRenderer(BlockEntityRendererProvider.Context context) {
this.itemRenderer = context.getItemRenderer();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fuzs.visualworkbench.compat.jei;
package fuzs.visualworkbench.integration.jei;

import fuzs.visualworkbench.VisualWorkbench;
import fuzs.visualworkbench.init.ModRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,24 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@SuppressWarnings("deprecation")
@Mixin(CraftingTableBlock.class)
public abstract class CraftingTableBlockMixin extends Block implements EntityBlock, VisualCraftingTableBlock {
abstract class CraftingTableBlockMixin extends Block implements EntityBlock, VisualCraftingTableBlock {

public CraftingTableBlockMixin(Properties p_i48440_1_) {
super(p_i48440_1_);
public CraftingTableBlockMixin(Properties properties) {
super(properties);
}

@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
return this.hasBlockEntity() ? new CraftingTableBlockEntity(pPos, pState) : null;
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return this.hasBlockEntity() ? new CraftingTableBlockEntity(pos, state) : null;
}

@Override
@Nullable
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level world, BlockState state, BlockEntityType<T> type) {
return checkType(type, ModRegistry.CRAFTING_TABLE_BLOCK_ENTITY.get(), CraftingTableBlockEntity::tick);
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
return level.isClientSide ? checkType(type, ModRegistry.CRAFTING_TABLE_BLOCK_ENTITY.get(), CraftingTableBlockEntity::clientTick) : null;
}

@Nullable
Expand All @@ -59,19 +60,19 @@ public void setPlacedBy(Level world, BlockPos pos, BlockState state, LivingEntit
}

@Override
public void onRemove(BlockState state, Level world, BlockPos pos, BlockState newState, boolean isMoving) {
public void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean isMoving) {
if (!state.is(newState.getBlock())) {
if (this.hasBlockEntity() && world.getBlockEntity(pos) instanceof CraftingTableBlockEntity blockEntity) {
Containers.dropContents(world, pos, blockEntity);
if (this.hasBlockEntity() && level.getBlockEntity(pos) instanceof CraftingTableBlockEntity blockEntity) {
Containers.dropContents(level, pos, blockEntity);
}
}
super.onRemove(state, world, pos, newState, isMoving);
super.onRemove(state, level, pos, newState, isMoving);
}

@Override
public boolean triggerEvent(BlockState state, Level world, BlockPos pos, int id, int param) {
final boolean result = super.triggerEvent(state, world, pos, id, param);
if (this.hasBlockEntity() && world.getBlockEntity(pos) instanceof CraftingTableBlockEntity blockEntity) {
public boolean triggerEvent(BlockState state, Level level, BlockPos pos, int id, int param) {
final boolean result = super.triggerEvent(state, level, pos, id, param);
if (this.hasBlockEntity() && level.getBlockEntity(pos) instanceof CraftingTableBlockEntity blockEntity) {
return blockEntity.triggerEvent(id, param);
}
return result;
Expand All @@ -82,24 +83,9 @@ public boolean hasBlockEntity() {
return JsonConfigBuilder.INSTANCE.contains(this);
}

// this is done via an event in case a mod overrides this in their custom crafting table class, so we have a chance to be compatible if needed
// @Inject(method = "use", at = @At("HEAD"), cancellable = true)
// public void use$head(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit, CallbackInfoReturnable<InteractionResult> callbackInfo) {
// if (this.hasBlockEntity() && world.getBlockEntity(pos) instanceof CraftingTableBlockEntity blockEntity) {
// if (world.isClientSide) {
// callbackInfo.setReturnValue(InteractionResult.SUCCESS);
// } else {
// player.openMenu(blockEntity);
// player.awardStat(Stats.INTERACT_WITH_CRAFTING_TABLE);
// callbackInfo.setReturnValue(InteractionResult.CONSUME);
// }
// }
// }

// not sure if this is even needed
@Inject(method = "getMenuProvider", at = @At("HEAD"), cancellable = true)
public void getMenuProvider$head(BlockState state, Level world, BlockPos pos, CallbackInfoReturnable<MenuProvider> callbackInfo) {
if (this.hasBlockEntity() && world.getBlockEntity(pos) instanceof CraftingTableBlockEntity blockEntity) {
public void getMenuProvider(BlockState state, Level level, BlockPos pos, CallbackInfoReturnable<MenuProvider> callbackInfo) {
if (this.hasBlockEntity() && level.getBlockEntity(pos) instanceof CraftingTableBlockEntity blockEntity) {
callbackInfo.setReturnValue(blockEntity);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
public interface CraftingMenuAccessor {

@Mutable
@Accessor
void setCraftSlots(CraftingContainer craftSlots);
@Accessor("craftSlots")
void visualworkbench$setCraftSlots(CraftingContainer craftSlots);

@Accessor
ResultContainer getResultSlots();
@Accessor("resultSlots")
ResultContainer visualworkbench$getResultSlots();
}
13 changes: 13 additions & 0 deletions Common/src/main/java/fuzs/visualworkbench/proxy/ClientProxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fuzs.visualworkbench.proxy;

import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockAndTintGetter;

public class ClientProxy extends ServerProxy {

@Override
public int getLightColor(BlockAndTintGetter blockAndTintGetter, BlockPos pos) {
return LevelRenderer.getLightColor(blockAndTintGetter, pos);
}
}
12 changes: 12 additions & 0 deletions Common/src/main/java/fuzs/visualworkbench/proxy/Proxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fuzs.visualworkbench.proxy;

import fuzs.puzzleslib.core.DistTypeExecutor;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockAndTintGetter;

public interface Proxy {
@SuppressWarnings("Convert2MethodRef")
Proxy INSTANCE = DistTypeExecutor.getForDistType(() -> () -> new ClientProxy(), () -> () -> new ServerProxy());

int getLightColor(BlockAndTintGetter blockAndTintGetter, BlockPos pos);
}
12 changes: 12 additions & 0 deletions Common/src/main/java/fuzs/visualworkbench/proxy/ServerProxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fuzs.visualworkbench.proxy;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockAndTintGetter;

public class ServerProxy implements Proxy {

@Override
public int getLightColor(BlockAndTintGetter blockAndTintGetter, BlockPos pos) {
return 15728880;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fuzs.visualworkbench.util;

public class MathHelper {

public static float easeOutQuad(double t, float b, float c, double d) {
float z = (float) t / (float) d;
return -c * z * (z - 2.0F) + b;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@
import net.minecraft.world.item.ItemStack;

public class CraftingContainerWrapper extends CraftingContainer {
private final Container inventory;
private final Container container;
private final AbstractContainerMenu menu;

public CraftingContainerWrapper(Container inventory, AbstractContainerMenu eventHandler, int width, int height) {
public CraftingContainerWrapper(Container container, AbstractContainerMenu eventHandler, int width, int height) {
super(eventHandler, width, height);
this.inventory = inventory;
this.container = container;
this.menu = eventHandler;
if (width * height != this.getContainerSize()) throw new IllegalArgumentException("Wrong crafting inventory dimensions!");
}

@Override
public int getContainerSize() {
return this.inventory.getContainerSize();
return this.container.getContainerSize();
}

@Override
public boolean isEmpty() {
return this.inventory.isEmpty();
return this.container.isEmpty();
}

@Override
public ItemStack getItem(int index) {
return this.inventory.getItem(index);
return this.container.getItem(index);
}

@Override
public ItemStack removeItemNoUpdate(int index) {
return this.inventory.removeItemNoUpdate(index);
return this.container.removeItemNoUpdate(index);
}

@Override
public ItemStack removeItem(int index, int count) {
ItemStack itemstack = this.inventory.removeItem(index, count);
ItemStack itemstack = this.container.removeItem(index, count);
if (!itemstack.isEmpty()) {
this.menu.slotsChanged(this);
}
Expand All @@ -49,23 +49,23 @@ public ItemStack removeItem(int index, int count) {

@Override
public void setItem(int index, ItemStack stack) {
this.inventory.setItem(index, stack);
this.container.setItem(index, stack);
this.menu.slotsChanged(this);
}

@Override
public void setChanged() {
this.inventory.setChanged();
this.container.setChanged();
}

@Override
public boolean stillValid(Player player) {
return this.inventory.stillValid(player);
return this.container.stillValid(player);
}

@Override
public void clearContent() {
this.inventory.clearContent();
this.container.clearContent();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,24 @@ public class ModCraftingMenu extends CraftingMenu implements ContainerListener {
private final Player player;
private final Consumer<ItemStack> containerData;

public ModCraftingMenu(int id, Inventory playerInventory) {
this(id, playerInventory, new SimpleContainer(9), ContainerLevelAccess.NULL, stack -> {});
public ModCraftingMenu(int id, Inventory inventory) {
this(id, inventory, new SimpleContainer(9), ContainerLevelAccess.NULL, stack -> {});
}

public ModCraftingMenu(int id, Inventory playerInventory, Container tileInventory, ContainerLevelAccess access, Consumer<ItemStack> containerData) {
super(id, playerInventory, access);
this.craftSlots = new CraftingContainerWrapper(tileInventory, this, 3, 3);
this.resultSlots = ((CraftingMenuAccessor) this).getResultSlots();
public ModCraftingMenu(int id, Inventory inventory, Container container, ContainerLevelAccess access, Consumer<ItemStack> containerData) {
super(id, inventory, access);
this.craftSlots = new CraftingContainerWrapper(container, this, 3, 3);
this.resultSlots = ((CraftingMenuAccessor) this).visualworkbench$getResultSlots();
this.access = access;
this.player = playerInventory.player;
this.player = inventory.player;
this.containerData = containerData;
((CraftingMenuAccessor) this).setCraftSlots(this.craftSlots);
this.slots.set(0, Util.make(new ResultSlot(playerInventory.player, this.craftSlots, this.resultSlots, 0, 124, 35) {
((CraftingMenuAccessor) this).visualworkbench$setCraftSlots(this.craftSlots);
this.slots.set(0, Util.make(new ResultSlot(inventory.player, this.craftSlots, this.resultSlots, 0, 124, 35) {

@Override
public void set(ItemStack p_75215_1_) {
public void set(ItemStack stack) {
// fast workbench makes this do nothing (via mixin), but in our case it is needed to avoid client desync
this.container.setItem(this.getContainerSlot(), p_75215_1_);
this.container.setItem(this.getContainerSlot(), stack);
this.setChanged();
}
}, slot -> slot.index = 0));
Expand Down Expand Up @@ -109,24 +110,24 @@ public void removed(Player player) {
* copied from vanilla super to avoid FastWorkbench mixin which would lead to a ClassCastException for craftSlots
*/
@Override
public ItemStack quickMoveStack(Player pPlayer, int pIndex) {
public ItemStack quickMoveStack(Player player, int index) {
ItemStack itemstack = ItemStack.EMPTY;
Slot slot = this.slots.get(pIndex);
Slot slot = this.slots.get(index);
if (slot != null && slot.hasItem()) {
ItemStack itemstack1 = slot.getItem();
itemstack = itemstack1.copy();
if (pIndex == 0) {
if (index == 0) {
this.access.execute((p_39378_, p_39379_) -> {
itemstack1.getItem().onCraftedBy(itemstack1, p_39378_, pPlayer);
itemstack1.getItem().onCraftedBy(itemstack1, p_39378_, player);
});
if (!this.moveItemStackTo(itemstack1, 10, 46, true)) {
return ItemStack.EMPTY;
}

slot.onQuickCraft(itemstack1, itemstack);
} else if (pIndex >= 10 && pIndex < 46) {
} else if (index >= 10 && index < 46) {
if (!this.moveItemStackTo(itemstack1, 1, 10, false)) {
if (pIndex < 37) {
if (index < 37) {
if (!this.moveItemStackTo(itemstack1, 37, 46, false)) {
return ItemStack.EMPTY;
}
Expand All @@ -148,9 +149,9 @@ public ItemStack quickMoveStack(Player pPlayer, int pIndex) {
return ItemStack.EMPTY;
}

slot.onTake(pPlayer, itemstack1);
if (pIndex == 0) {
pPlayer.drop(itemstack1, false);
slot.onTake(player, itemstack1);
if (index == 0) {
player.drop(itemstack1, false);
}
}

Expand Down
Loading

0 comments on commit 9d3d1e4

Please sign in to comment.