Skip to content

Commit

Permalink
Added on texture for crusher. Added ore scanning debug item
Browse files Browse the repository at this point in the history
  • Loading branch information
RebelKeithy committed Aug 20, 2021
1 parent 382fd95 commit 29268be
Show file tree
Hide file tree
Showing 23 changed files with 365 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package com.teammetallurgy.metallurgyclassic;

import com.teammetallurgy.metallurgyclassic.client.render.entities.CustomizableTntRenderer;
import com.teammetallurgy.metallurgyclassic.debug.OreScannerKeybind;
import com.teammetallurgy.metallurgyclassic.machines.chest.MetalChestComponent;
import com.teammetallurgy.metallurgyclassic.machines.crusher.CrusherComponent;
import com.teammetallurgy.metallurgyclassic.machines.furnace.MetalFurnaceComponent;
import com.teammetallurgy.metallurgyclassic.network.CustomizableTntSpawnPacket;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry;
import net.fabricmc.fabric.api.client.rendereregistry.v1.EntityRendererRegistry;
import net.fabricmc.fabric.api.event.client.ClientSpriteRegistryCallback;
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
import net.fabricmc.fabric.api.particle.v1.FabricParticleTypes;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.particle.ParticleTextureSheet;
import net.minecraft.client.particle.SpriteBillboardParticle;
import net.minecraft.client.texture.SpriteAtlasTexture;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.particle.DefaultParticleType;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;

import static com.teammetallurgy.metallurgyclassic.MetallurgyClassic.id;

@Environment(EnvType.CLIENT)
public class MetallurgyClassicClient implements ClientModInitializer {
Expand All @@ -22,5 +36,7 @@ public void onInitializeClient() {
MetalFurnaceComponent.onInitializerClient();
MetalChestComponent.onInitializerClient();
CrusherComponent.onInitializerClient();

OreScannerKeybind.setup();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.teammetallurgy.metallurgyclassic.debug;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

public class OreScannerItem extends Item {

public OreScannerItem(Settings settings) {
super(settings);
}

@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
ItemStack itemStack = user.getStackInHand(hand);
user.openHandledScreen(new NamedScreenHandlerFactory() {
@Override
public Text getDisplayName() {
return Text.of("Ore Scanner");
}

@Nullable
@Override
public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) {
return new OreScannerScreenHandler(syncId, inv);
}
});

// for(int x = 0; x < 16; x++) {
// for(int y = 0; y < 64; y++) {
// for(int z = 0; z < 16; z++) {
// BlockPos pos = new BlockPos(user.getChunkPos().x * 16 + x, y, user.getChunkPos().z * 16 + z);
// BlockPos pos2 = new BlockPos(user.getChunkPos().x * 16 + x, y + 64, user.getChunkPos().z * 16 + z);
// BlockState state = world.getBlockState(pos);
// if(state.toString().contains("ore") && state.toString().contains("metallurgy")) {
// world.setBlockState(pos2, world.getBlockState(pos));
// }
// }
// }
// }

return TypedActionResult.success(itemStack);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.teammetallurgy.metallurgyclassic.debug;

import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry;
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;

import static com.teammetallurgy.metallurgyclassic.MetallurgyClassic.MOD_ID;
import static com.teammetallurgy.metallurgyclassic.MetallurgyClassic.id;

public class OreScannerKeybind {
public static ScreenHandlerType<OreScannerScreenHandler> ORE_SCANNER_SCREEN_HANDLER = ScreenHandlerRegistry.registerSimple(id("ore_scanner"), OreScannerScreenHandler::new);

public static void setup() {
ScreenRegistry.register(ORE_SCANNER_SCREEN_HANDLER, OreScannerScreen::new);
var keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding(
MOD_ID + ".openscreen",
InputUtil.Type.KEYSYM,
GLFW.GLFW_KEY_R,
"category." + MOD_ID
));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while(keyBinding.wasPressed()) {
System.out.println("Key was pressed");

client.player.openHandledScreen(new NamedScreenHandlerFactory() {
@Override
public Text getDisplayName() {
return Text.of("Ore Scanner");
}

@Nullable
@Override
public ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player) {
return new OreScannerScreenHandler(ORE_SCANNER_SCREEN_HANDLER, syncId);
}
});
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.teammetallurgy.metallurgyclassic.debug;

import com.mojang.blaze3d.systems.RenderSystem;
import com.teammetallurgy.metallurgyclassic.utils.NineSlice;
import net.minecraft.block.Block;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.BlockItem;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;

import java.awt.*;
import java.util.*;
import java.util.List;

import static com.teammetallurgy.metallurgyclassic.MetallurgyClassic.id;

public class OreScannerScreen extends HandledScreen<OreScannerScreenHandler> {
private final NineSlice background = new NineSlice(id("textures/gui/gui_background.png"), 4, 16, 16);
private final Identifier SLOT = id("textures/gui/slot_background.png");
private final NineSlice bar = new NineSlice(id("textures/gui/slot_background.png"), 1, 16, -18, -18);
private final NineSlice slot = new NineSlice(id("textures/gui/slot_background.png"), 6, 18, 18);

public OreScannerScreen(OreScannerScreenHandler handler, PlayerInventory inventory, Text title) {
super(handler, inventory, title);
this.backgroundWidth = 176;
this.backgroundHeight = 266;
this.playerInventoryTitleY = 10000;
}

@Override
protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
background.renderCentered(matrices, this.backgroundWidth, this.backgroundHeight);

RenderSystem.setShaderTexture(0, SLOT);
int i = (this.width - this.backgroundWidth) / 2;
int j = (this.height - this.backgroundHeight) / 2;

List<Block> countables = new ArrayList<>();
for(Slot slot : this.handler.slots) {
if(slot.getStack() != null && slot.getStack().getItem() != null && slot.getStack().getItem() instanceof BlockItem) {
Block block = ((BlockItem)(slot.getStack().getItem())).getBlock();
countables.add(block);
}
}

Map<Block, Integer> counts = getBlockCount();
Optional<Integer> maxCount = this.handler.slots.stream()
.filter(slot -> slot instanceof OreScannerScreenHandler.DisplaySlot)
.filter(slot -> !slot.getStack().isEmpty())
.filter(slot -> slot.getStack().getItem() instanceof BlockItem)
.filter(slot -> counts.containsKey(((BlockItem) slot.getStack().getItem()).getBlock()))
.map(slot -> counts.get(((BlockItem) slot.getStack().getItem()).getBlock()))
.max(Comparator.naturalOrder());

int maxValue = 200;
if(maxCount.isPresent()) {
//maxValue = maxCount.get();
}

int slotY = 266 - 5 - 18 - 5;
int slot_height = 220 - 20;
for(Slot slot : this.handler.slots) {
drawTexture(matrices, i + slot.x - 1, j + slot.y - 1, 0, 0, 18, 18, 18, 18);
}
for(Slot slot : this.handler.slots) {
if (slot instanceof OreScannerScreenHandler.DisplaySlot) {
if (slot.getStack() != null && slot.getStack().getItem() != null && slot.getStack().getItem() instanceof BlockItem) {
if(!slot.getStack().isEmpty()) {
Block block = ((BlockItem) (slot.getStack().getItem())).getBlock();
int count = 0;
if(counts.containsKey(block)) {
count = counts.get(block);
}
drawBar(matrices, x + slot.x, y + slot.y - 1, slot_height, count * 200/maxValue);
} else {
drawBar(matrices, x + slot.x, y + slot.y - 1, slot_height, 0);
}
}
}
}
}

private void drawBar(MatrixStack matrices, int x, int y, int height, int value) {
int min = 4;
slot.render(matrices, x, y - height, 16, height);
if(value > -1) {
int renderedValue = value + 4;
if(value <= 0) {
RenderSystem.setShaderColor(0.9f, 0.1f, 0.1f, 1.0f);
} else if (value < min) {
RenderSystem.setShaderColor(1f, 0.7f, 0.2f, 1.0f);
} else {
RenderSystem.setShaderColor(0.1f, 0.9f, 0.1f, 1.0f);
}
bar.render(matrices, x + 1, y - renderedValue - 1, 14, renderedValue);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
String text = "" + value;
drawCenteredText(matrices, textRenderer, text, x + 8, y - 11, Color.WHITE.getRGB());
}
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
}

private Map<Block, Integer> getBlockCount() {
var counts = new HashMap<Block, Integer>();
if(client.world != null && client.player != null) {
for(int x = 0; x < 16; x++) {
for(int y = 0; y < 256; y++) {
for(int z = 0; z < 16; z++) {
BlockPos pos = new BlockPos(client.player.getChunkPos().x * 16 + x, y, client.player.getChunkPos().z * 16 + z);
Block block = client.world.getBlockState(pos).getBlock();

if(counts.containsKey(block)) {
counts.put(block, counts.get(block) + 1);
} else {
counts.put(block, 1);
}
}
}
}
}
return counts;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.teammetallurgy.metallurgyclassic.debug;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;

import java.util.ArrayList;
import java.util.List;

public class OreScannerScreenHandler extends ScreenHandler {

static List<DisplaySlot> scanSlots = new ArrayList<>();

static {
int slotY = 266 - 5 - 18;
int scannerOffset = -22;
scanSlots.add(new DisplaySlot(Items.IRON_ORE.getDefaultStack(), 8, slotY + scannerOffset));
scanSlots.add(new DisplaySlot(Items.DIAMOND_ORE.getDefaultStack(), 8 + 18, slotY + scannerOffset));
scanSlots.add(new DisplaySlot(Items.COAL_ORE.getDefaultStack(), 8 + 18 * 2, slotY + scannerOffset));
scanSlots.add(new DisplaySlot(Items.DIRT.getDefaultStack(), 8 + 18 * 3, slotY + scannerOffset));
scanSlots.add(new DisplaySlot(Items.GRAVEL.getDefaultStack(), 8 + 18 * 4, slotY + scannerOffset));
scanSlots.add(new DisplaySlot(ItemStack.EMPTY, 8 + 18 * 5, slotY + scannerOffset));
scanSlots.add(new DisplaySlot(ItemStack.EMPTY, 8 + 18 * 6, slotY + scannerOffset));
scanSlots.add(new DisplaySlot(ItemStack.EMPTY, 8 + 18 * 7, slotY + scannerOffset));
scanSlots.add(new DisplaySlot(ItemStack.EMPTY, 8 + 18 * 8, slotY + scannerOffset));
}

public static class DisplaySlot extends Slot {
public DisplaySlot(ItemStack itemStack, int x, int y) {
super(new SimpleInventory(itemStack), 0, x, y);
}

@Override
public boolean canInsert(ItemStack stack) {
return false;
}

@Override
public boolean canTakeItems(PlayerEntity playerEntity) {
return false;
}

// @Override
// public ItemStack insertStack(ItemStack stack, int count) {
// return stack;
// }
}

public OreScannerScreenHandler(int syncId, PlayerInventory playerInventory) {
this(OreScannerKeybind.ORE_SCANNER_SCREEN_HANDLER, syncId);


int slotY = 266 - 5 - 18;
for(int y = 0; y < 9; ++y) {
this.addSlot(new Slot(playerInventory, y, 8 + y * 18, slotY));
}
scanSlots.forEach(this::addSlot);
}

protected OreScannerScreenHandler(ScreenHandlerType<?> type, int syncId) {
super(type, syncId);
}

@Override
public boolean canUse(PlayerEntity player) {
return true;
}

@Override
public void onSlotClick(int slotIndex, int button, SlotActionType actionType, PlayerEntity player) {
try {
if(slots.get(slotIndex) instanceof DisplaySlot) {
slots.get(slotIndex).setStack(this.getCursorStack().copy());
} else {
}
} catch(Exception e) {
System.out.println(e);
}
super.onSlotClick(slotIndex, button, actionType, player);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.teammetallurgy.metallurgyclassic.items;

import com.teammetallurgy.metallurgyclassic.debug.OreScannerItem;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.minecraft.item.BoneMealItem;
import net.minecraft.item.Item;
Expand All @@ -14,12 +15,14 @@ public class MetallurgyItems {
public static IgniterItem MATCH;
public static Item TAR;
public static Item FERTILIZER;
public static Item DEBUG_STICK;

public static void initialize() {
MAGNESIUM_IGNITER = (IgniterItem) register("magnesium_igniter", new IgniterItem(new FabricItemSettings().maxDamage(256).group(ItemGroup.TOOLS)));
MATCH = (IgniterItem) register("match", new IgniterItem(new FabricItemSettings().maxDamage(1).group(ItemGroup.TOOLS)));
TAR = register("tar", new Item(new FabricItemSettings().group(ItemGroup.MISC)));
FERTILIZER = register("fertilizer", new BoneMealItem(new FabricItemSettings().group(ItemGroup.MISC)));
DEBUG_STICK = register("debug_stick", new OreScannerItem(new FabricItemSettings().group(ItemGroup.MISC)));

MAGNESIUM_IGNITER.registerDispenserBehavior();
MATCH.registerDispenserBehavior();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void onInitializerClient() {

for(Type type : Type.values()) {
SpriteIdentifier spriteIdentifier = new SpriteIdentifier(CHEST_ATLAS_TEXTURE, id("entity/crusher/crusher_" + type.name));
SpriteIdentifier spriteIdentifierLit = new SpriteIdentifier(CHEST_ATLAS_TEXTURE, id("entity/crusher/crusher_" + type.name + "_lit"));
SpriteIdentifier spriteIdentifierLit = new SpriteIdentifier(CHEST_ATLAS_TEXTURE, id("entity/crusher/crusher_" + type.name + "_on"));
ClientSpriteRegistryCallback.event(CHEST_ATLAS_TEXTURE).register((atlasTexture, registry) -> {
registry.register(spriteIdentifier.getTextureId());
registry.register(spriteIdentifierLit.getTextureId());
Expand Down
Loading

0 comments on commit 29268be

Please sign in to comment.