Skip to content

Commit

Permalink
more 1.21 port (still WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Aug 9, 2024
1 parent 90f7d4f commit 965f2b9
Show file tree
Hide file tree
Showing 22 changed files with 178 additions and 260 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/traverse/bhc/client/HeartCanistersClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.traverse.bhc.client;

import com.traverse.bhc.client.screens.BladeOfVitalityScreen;
import com.traverse.bhc.client.screens.HeartAmuletScreen;
import com.traverse.bhc.client.screens.SoulHeartAmuletScreen;
import com.traverse.bhc.common.BaubleyHeartCanisters;
import com.traverse.bhc.common.init.RegistryHandler;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;

@EventBusSubscriber(modid = BaubleyHeartCanisters.MODID, bus = EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
public class HeartCanistersClient {

@SubscribeEvent
public static void registerScreens(RegisterMenuScreensEvent event) {
event.register(RegistryHandler.HEART_AMUlET_CONTAINER.get(), HeartAmuletScreen::new);
event.register(RegistryHandler.SOUL_HEART_AMUlET_CONTAINER.get(), SoulHeartAmuletScreen::new);
event.register(RegistryHandler.BLADE_OF_VITALITY_CONTAINER.get(), BladeOfVitalityScreen::new);
}
}
24 changes: 0 additions & 24 deletions src/main/java/com/traverse/bhc/client/proxy/ClientProxy.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class BladeOfVitalityScreen extends AbstractContainerScreen<BladeOfVitalityContainer> {

private static final ResourceLocation BACKGROUND_TEXTURE = new ResourceLocation(BaubleyHeartCanisters.MODID, "textures/gui/blade_of_vitality.png");
private static final ResourceLocation BACKGROUND_TEXTURE = BaubleyHeartCanisters.id("textures/gui/blade_of_vitality.png");

public BladeOfVitalityScreen(BladeOfVitalityContainer container, Inventory inventory, Component title) {
super(container, inventory, title);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@OnlyIn(Dist.CLIENT)
public class HeartAmuletScreen extends AbstractContainerScreen<HeartAmuletContainer> {

private static final ResourceLocation BACKGROUND_TEXTURE = new ResourceLocation(BaubleyHeartCanisters.MODID, "textures/gui/heart_amulet.png");
private static final ResourceLocation BACKGROUND_TEXTURE = BaubleyHeartCanisters.id("textures/gui/heart_amulet.png");

public HeartAmuletScreen(HeartAmuletContainer container, Inventory inventory, Component title) {
super(container, inventory, title);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.traverse.bhc.client.screens;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.traverse.bhc.common.BaubleyHeartCanisters;
import com.traverse.bhc.common.container.SoulHeartAmuletContainer;
import net.minecraft.client.gui.GuiGraphics;
Expand All @@ -16,7 +15,7 @@

@OnlyIn(Dist.CLIENT)
public class SoulHeartAmuletScreen extends AbstractContainerScreen<SoulHeartAmuletContainer> {
private static final ResourceLocation BACKGROUND_TEXTURE = new ResourceLocation(BaubleyHeartCanisters.MODID, "textures/gui/soul_heart_amulet.png");
private static final ResourceLocation BACKGROUND_TEXTURE = BaubleyHeartCanisters.id("textures/gui/soul_heart_amulet.png");

public SoulHeartAmuletScreen(SoulHeartAmuletContainer container, Inventory inventory, Component title) {
super(container, inventory, title);
Expand Down
73 changes: 41 additions & 32 deletions src/main/java/com/traverse/bhc/common/BaubleyHeartCanisters.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,86 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.traverse.bhc.client.proxy.ClientProxy;
import com.traverse.bhc.common.config.BHCConfig;
import com.traverse.bhc.common.config.ConfigHandler;
import com.traverse.bhc.common.init.RegistryHandler;
import com.traverse.bhc.common.proxy.CommonProxy;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.DistExecutor;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.config.ModConfig;
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
import net.neoforged.fml.event.lifecycle.InterModEnqueueEvent;
import net.neoforged.fml.loading.FMLPaths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;

@Mod(BaubleyHeartCanisters.MODID)
public class BaubleyHeartCanisters {

public static final String MODID = "bhc";

public static BHCConfig config;
public static final CommonProxy proxy = DistExecutor.safeRunForDist(() -> ClientProxy::new, () -> CommonProxy::new);
private static final Logger LOGGER = LoggerFactory.getLogger(BaubleyHeartCanisters.class);

public BaubleyHeartCanisters(IEventBus eventBus) {
RegistryHandler.ITEMS.register(eventBus);
RegistryHandler.TAB.register(eventBus);
RegistryHandler.CONTAINERS.register(eventBus);
RegistryHandler.RECIPESERIALIZER.register(eventBus);
ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, ConfigHandler.configSpec);
ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, ConfigHandler.serverConfigSpec);
public static BHCConfig config;

eventBus.addListener(this::setup);
eventBus.addListener(this::enqueue);
public BaubleyHeartCanisters(IEventBus modEventBus, ModContainer modContainer) {
RegistryHandler.ITEMS.register(modEventBus);
RegistryHandler.TAB.register(modEventBus);
RegistryHandler.CONTAINERS.register(modEventBus);
RegistryHandler.RECIPESERIALIZER.register(modEventBus);
modContainer.registerConfig(ModConfig.Type.COMMON, ConfigHandler.configSpec);
modContainer.registerConfig(ModConfig.Type.SERVER, ConfigHandler.serverConfigSpec);

modEventBus.addListener(this::setup);
modEventBus.addListener(this::interModComms);
}

private void setup(final FMLCommonSetupEvent event) {
proxy.doClientStuff();
jsonSetup();
}

private void enqueue(InterModEnqueueEvent event) {
private void interModComms(InterModEnqueueEvent event) {
// InterModComms.sendTo(CuriosApi.MODID, SlotTypeMessage.REGISTER_TYPE, () -> new SlotTypeMessage.Builder("heartamulet").icon(ClientBaubleyHeartCanisters.SLOT_TEXTURE).build());
}

private void jsonSetup() {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
File folder = FMLPaths.CONFIGDIR.get().resolve("bhc").toFile();
folder.mkdirs();
File file = folder.toPath().resolve("drops.json").toFile();
var configFile = FMLPaths.CONFIGDIR.get().resolve("bhc").resolve("drops.json");

try {
Files.createDirectories(configFile.getParent());
} catch (IOException e) {
throw new RuntimeException("unable to create %s config directory".formatted(BaubleyHeartCanisters.MODID), e);
}

try {
if (file.exists()) {
config = gson.fromJson(new FileReader(file), BHCConfig.class);
if (Files.exists(configFile)) {
try (var reader = Files.newBufferedReader(configFile)) {
config = gson.fromJson(reader, BHCConfig.class);
}
return;
}
config = new BHCConfig();
} catch (IOException e) {
LOGGER.error("Unable to read config file", e);
}

config = new BHCConfig();
try (var writer = Files.newBufferedWriter(configFile)) {
config.addEntrytoMap("red", "hostile", 0.05);
config.addEntrytoMap("yellow", "boss", 1.0);
config.addEntrytoMap("green", "dragon", 1.0);
config.addEntrytoMap("blue", "minecraft:warden", 1.0);
String json = gson.toJson(config, BHCConfig.class);
FileWriter writer = new FileWriter(file);
writer.write(json);
writer.flush();
writer.close();
gson.toJson(config, writer);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Unable to write %s config".formatted(BaubleyHeartCanisters.MODID), e);
}
}

public static ResourceLocation id(String path) {
return ResourceLocation.fromNamespaceAndPath(MODID, path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public static class General {
}

public static class BHCServer {
public final ModConfigSpec.ConfigValue<Boolean> allowStartingHeathTweaks;
public final ModConfigSpec.ConfigValue<Boolean> allowStartingHealthTweaks;
public final ModConfigSpec.ConfigValue<Integer> startingHealth;

BHCServer(ModConfigSpec.Builder builder) {
builder.push("Server");
allowStartingHeathTweaks = builder
allowStartingHealthTweaks = builder
.comment("Allow Starting Health Tweaks")
.define("allowStartingHealthTweaks", false);
startingHealth = builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@
import com.traverse.bhc.common.init.RegistryHandler;
import com.traverse.bhc.common.items.BaseHeartCanister;
import com.traverse.bhc.common.util.InventoryUtil;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.Container;
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.Slot;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.ItemStackHandler;
import net.neoforged.neoforge.items.IItemHandlerModifiable;
import net.neoforged.neoforge.items.SlotItemHandler;


import javax.annotation.Nonnull;
import java.awt.*;
import java.util.List;

public class BladeOfVitalityContainer extends AbstractContainerMenu {
DataComponentType<Integer> HEART_AMOUNT_COMPONENT = DataComponentType.<Integer>builder().build();
public ItemStackHandler itemStackHandler;

public static final int SLOT_COUNT = 4;

DataComponentType<List<Integer>> HEART_AMOUNT_COMPONENT = DataComponentType.<List<Integer>>builder().build();
public IItemHandlerModifiable itemStackHandler;

public BladeOfVitalityContainer(int windowId, Inventory playerInventory, ItemStack stack) {
super(RegistryHandler.BLADE_OF_VITALITY_CONTAINER.get(), windowId);
this.itemStackHandler = InventoryUtil.createVirtualInventory(4, stack);
this.itemStackHandler = InventoryUtil.createVirtualInventory(SLOT_COUNT, stack);


//Heart Container Slots
this.addSlot(new BladeOfVitalityContainer.SlotPendant(this.itemStackHandler, 0, 80, 5));//RED
this.addSlot(new BladeOfVitalityContainer.SlotPendant(this.itemStackHandler, 1, 80, 25));//YELLOW
this.addSlot(new BladeOfVitalityContainer.SlotPendant(this.itemStackHandler, 2, 80, 45));//GREEN
this.addSlot(new BladeOfVitalityContainer.SlotPendant(this.itemStackHandler, 3, 80, 65));//BLUE
for (int i = 0; i < SLOT_COUNT; i++) {
int y = i * 20 + 5;
this.addSlot(new BladeOfVitalityContainer.SlotPendant(this.itemStackHandler, i, 80, y));
}

//Add player inventory slots
for (int row = 0; row < 9; ++row) {
Expand All @@ -48,7 +48,7 @@ public BladeOfVitalityContainer(int windowId, Inventory playerInventory, ItemSta
addSlot(new Slot(playerInventory, row, x, y));
}

for (int row = 1; row < 4; ++row) {
for (int row = 1; row < SLOT_COUNT; ++row) {
for (int col = 0; col < 9; ++col) {
int x = 8 + col * 18;
int y = row * 18 + (56 + 10);
Expand All @@ -57,25 +57,6 @@ public BladeOfVitalityContainer(int windowId, Inventory playerInventory, ItemSta
}
}

@Override
public void removed(Player playerIn) {
ItemStack sword = playerIn.getMainHandItem();
InventoryUtil.serializeInventory(this.itemStackHandler, sword);



DataComponentType<Integer> nbt = sword.getOrDefault(HEART_AMOUNT_COMPONENT, 0);
int[] hearts = new int[this.itemStackHandler.getSlots()];
for (int i = 0; i < hearts.length; i++) {
ItemStack stack = this.itemStackHandler.getStackInSlot(i);
if (!stack.isEmpty()) hearts[i] = stack.getCount() * 2;
}
nbt.putIntArray(HEART_AMOUNT, hearts);
sword.setTag(nbt);

super.removed(playerIn);
}

@Override
public boolean stillValid(Player playerIn) {
return true;
Expand Down
Loading

0 comments on commit 965f2b9

Please sign in to comment.