Skip to content

Commit

Permalink
fix: don't put client code in common locations...
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHillcox committed Mar 5, 2023
1 parent 6ab05cf commit b5b1267
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.direwolf20.buildinggadgets.common.blocks.ConstructionBlock;
import com.direwolf20.buildinggadgets.common.blocks.OurBlocks;
import com.direwolf20.buildinggadgets.common.containers.OurContainers;
import com.direwolf20.buildinggadgets.common.items.ConstructionPasteContainer;
import com.direwolf20.buildinggadgets.common.items.GadgetCopyPaste;
import com.direwolf20.buildinggadgets.common.items.OurItems;
import com.direwolf20.buildinggadgets.common.tileentities.ConstructionBlockTileEntity;
Expand All @@ -18,6 +19,7 @@
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.ItemOverrides;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelResourceLocation;
Expand All @@ -26,6 +28,7 @@
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.level.block.Blocks;
Expand Down Expand Up @@ -65,6 +68,18 @@ public static void clientSetup() {

MinecraftForge.EVENT_BUS.addListener(ClientProxy::onPlayerLoggedOut);
CACHE_TEMPLATE_PROVIDER.registerUpdateListener(((GadgetCopyPaste) OurItems.COPY_PASTE_GADGET_ITEM.get()).getRender());

List.of(
OurItems.PASTE_CONTAINER_T1_ITEM,
OurItems.PASTE_CONTAINER_T2_ITEM,
OurItems.PASTE_CONTAINER_T3_ITEM,
OurItems.PASTE_CONTAINER_CREATIVE_ITEM
).forEach(item -> {
ItemProperties.register(item.get(), ConstructionPasteContainer.LEVEL, (stack, clientLevel, entity, notSure) -> {
float percent = ConstructionPasteContainer.getPasteAmount(stack) / (float) ConstructionPasteContainer.getMaxPasteAmount(stack);
return Mth.floor(percent * 4) / 4F;
});
});
}

@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,15 @@ public static int getPasteAmount(ItemStack stack) {
Item item = stack.getItem();
if (item instanceof ConstructionPasteContainer)
return ((ConstructionPasteContainer) item).getPasteCount(stack);
BuildingGadgets.LOG.warn("Potential abuse of ConstructionPasteContainer#getPasteAmount(ItemStack) where the given ItemStack does not contain a ConstructionPasteContainer.");

return 0;
}

public static int getMaxPasteAmount(ItemStack stack) {
Item item = stack.getItem();
if (item instanceof ConstructionPasteContainer)
return ((ConstructionPasteContainer) item).getMaxCapacity();

return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
import com.direwolf20.buildinggadgets.common.blocks.OurBlocks;
import com.direwolf20.buildinggadgets.common.config.Config;
import com.direwolf20.buildinggadgets.common.util.ref.Reference;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.util.Mth;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

import java.util.function.BiFunction;
import java.util.function.Supplier;

public final class OurItems {
private OurItems() {}

Expand All @@ -25,22 +20,17 @@ private OurItems() {}
public static final RegistryObject<Item> COPY_PASTE_GADGET_ITEM = ITEMS.register("gadget_copy_paste", GadgetCopyPaste::new);
public static final RegistryObject<Item> DESTRUCTION_GADGET_ITEM = ITEMS.register("gadget_destruction", GadgetDestruction::new);

private static final BiFunction<Boolean, Supplier<Integer>, Supplier<? extends Item>> REGISTER_CONTAINER = (creative, config) -> () -> {
ConstructionPasteContainer constructionPasteContainer = new ConstructionPasteContainer(creative, config::get);

ItemProperties.register(constructionPasteContainer, ConstructionPasteContainer.LEVEL, (stack, clientLevel, entity, notSure) -> {
float percent = ConstructionPasteContainer.getPasteAmount(stack) / (float) config.get();
return Mth.floor(percent * 4) / 4F;
});
// Construction Paste Containers
public static final RegistryObject<Item> PASTE_CONTAINER_T1_ITEM = ITEMS.register("construction_paste_container_t1", () -> new ConstructionPasteContainer(false, Config.PASTE_CONTAINERS.capacityT1::get));

return constructionPasteContainer;
};
public static final RegistryObject<Item> PASTE_CONTAINER_T2_ITEM
= ITEMS.register("construction_paste_container_t2", () -> new ConstructionPasteContainer(false, Config.PASTE_CONTAINERS.capacityT2::get));
public static final RegistryObject<Item> PASTE_CONTAINER_T3_ITEM
= ITEMS.register("construction_paste_container_t3", () -> new ConstructionPasteContainer(false, Config.PASTE_CONTAINERS.capacityT3::get));
public static final RegistryObject<Item> PASTE_CONTAINER_CREATIVE_ITEM
= ITEMS.register("construction_paste_container_creative", () -> new ConstructionPasteContainer(true));

// Construction Paste Containers
public static final RegistryObject<Item> PASTE_CONTAINER_T1_ITEM = ITEMS.register("construction_paste_container_t1", REGISTER_CONTAINER.apply(false, Config.PASTE_CONTAINERS.capacityT1));
public static final RegistryObject<Item> PASTE_CONTAINER_T2_ITEM = ITEMS.register("construction_paste_container_t2", REGISTER_CONTAINER.apply(false, Config.PASTE_CONTAINERS.capacityT2));
public static final RegistryObject<Item> PASTE_CONTAINER_T3_ITEM = ITEMS.register("construction_paste_container_t3", REGISTER_CONTAINER.apply(false, Config.PASTE_CONTAINERS.capacityT3));
public static final RegistryObject<Item> PASTE_CONTAINER_CREATIVE_ITEM = ITEMS.register("construction_paste_container_creative", REGISTER_CONTAINER.apply(true, () -> Integer.MAX_VALUE));

// Construction Paste
public static final RegistryObject<Item> CONSTRUCTION_PASTE_ITEM = ITEMS.register("construction_paste", ConstructionPaste::new);
Expand Down

0 comments on commit b5b1267

Please sign in to comment.