Skip to content

Commit

Permalink
Clean up data API
Browse files Browse the repository at this point in the history
  • Loading branch information
legobmw99 committed Jan 14, 2024
1 parent 81e4892 commit 0d3400a
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.legobmw99.allomancy.api.enums.Metal;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.Level;

public interface IAllomancerData {
Expand All @@ -13,9 +12,9 @@ public interface IAllomancerData {
* amounts. Then syncs to the client to make sure everyone is on the same
* page
*
* @param player the player being checked
* @return whether an observable change was made
*/
void tickBurning(ServerPlayer player);
boolean tickBurning();

/**
* Get if the player has the supplied power
Expand Down Expand Up @@ -94,20 +93,26 @@ public interface IAllomancerData {
void setBurning(Metal metal, boolean metalBurning);

/**
* Sets the players amount of Metal to the given value
* Increase the amount of this metal being stored
*
* @param metal the Metal to set
* @param amt the amount stored
* @param metal
*/
void setAmount(Metal metal, int amt);
void incrementStored(Metal metal);

/**
* Decrease the amount of this metal being stored
*
* @param metal
*/
void decrementStored(Metal metal);

/**
* Gets the players stored amount of the given metal
*
* @param metal the Metal to check
* @return the amount stored
*/
int getAmount(Metal metal);
int getStored(Metal metal);

/**
* Drain all specified metals
Expand Down Expand Up @@ -178,7 +183,7 @@ public interface IAllomancerData {
*/
ResourceKey<Level> getSpawnDim();

void decEnhanced();
void decrementEnhanced();

boolean isEnhanced();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
import java.util.function.Supplier;

public class ConsumeSetup {
public static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(Allomancy.MODID);
private static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(Allomancy.MODID);
public static final DeferredItem<GrinderItem> ALLOMANTIC_GRINDER = ITEMS.register("allomantic_grinder", GrinderItem::new);
public static final DeferredItem<LerasiumItem> LERASIUM_NUGGET = ITEMS.register("lerasium_nugget", LerasiumItem::new);
public static final DeferredItem<VialItem> VIAL = ITEMS.register("vial", VialItem::new);

public static final DeferredRegister<RecipeSerializer<?>> RECIPES = DeferredRegister.create(Registries.RECIPE_SERIALIZER, Allomancy.MODID);
private static final DeferredRegister<RecipeSerializer<?>> RECIPES = DeferredRegister.create(Registries.RECIPE_SERIALIZER, Allomancy.MODID);
public static final Supplier<VialItemRecipe.Serializer> VIAL_RECIPE_SERIALIZER = RECIPES.register("vial_filling", VialItemRecipe.Serializer::new);

public static void register(IEventBus bus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.legobmw99.allomancy.api.enums.Metal;
import com.legobmw99.allomancy.modules.consumables.ConsumeSetup;
import com.legobmw99.allomancy.modules.powers.data.AllomancerAttachment;
import com.legobmw99.allomancy.modules.powers.data.AllomancerData;
import com.legobmw99.allomancy.util.ItemDisplay;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.screens.Screen;
Expand Down Expand Up @@ -40,8 +41,8 @@ public ItemStack finishUsingItem(ItemStack stack, Level world, LivingEntity livi
var data = livingEntity.getData(AllomancerAttachment.ALLOMANCY_DATA);
for (Metal mt : Metal.values()) {
if (stack.getTag().contains(mt.getName()) && stack.getTag().getBoolean(mt.getName())) {
if (data.getAmount(mt) < 10) {
data.setAmount(mt, data.getAmount(mt) + 1);
if (data.getStored(mt) < AllomancerData.MAX_STORAGE) {
data.incrementStored(mt);
}
}
}
Expand Down Expand Up @@ -79,7 +80,7 @@ public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, In
for (Metal mt : Metal.values()) {
if (itemStackIn.getTag().contains(mt.getName()) && itemStackIn.getTag().getBoolean(mt.getName())) {
filling++;
if (data.getAmount(mt) >= 10) {
if (data.getStored(mt) >= AllomancerData.MAX_STORAGE) {
full++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import java.util.function.Supplier;

public class ExtrasSetup {
public static final DeferredRegister.Blocks BLOCKS = DeferredRegister.createBlocks(Allomancy.MODID);
public static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(Allomancy.MODID);
public static final DeferredRegister<BannerPattern> BP = DeferredRegister.create(Registries.BANNER_PATTERN, Allomancy.MODID);
private static final DeferredRegister.Blocks BLOCKS = DeferredRegister.createBlocks(Allomancy.MODID);
private static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(Allomancy.MODID);
private static final DeferredRegister<BannerPattern> BP = DeferredRegister.create(Registries.BANNER_PATTERN, Allomancy.MODID);


public static final DeferredBlock<IronButtonBlock> IRON_BUTTON = BLOCKS.register("iron_button", () -> new IronButtonBlock(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static void onPlayerClone(final PlayerEvent.Clone event) {
// if they died and keepInventory isn't set, they shouldn't keep their metals.
var data = player.getData(AllomancerAttachment.ALLOMANCY_DATA);
for (Metal mt : Metal.values()) {
data.setAmount(mt, 0);
data.drainMetals(mt);
data.setBurning(mt, false);
}
}
Expand Down Expand Up @@ -253,32 +253,33 @@ public static void onWorldTick(final TickEvent.LevelTickEvent event) {
private static void playerPowerTick(Player curPlayer, Level level) {
var data = curPlayer.getData(AllomancerAttachment.ALLOMANCY_DATA);
if (!data.isUninvested()) {
/*********************************************
* ALUMINUM AND DURALUMIN *
*********************************************/
if (data.isBurning(Metal.ALUMINUM)) {
PowerUtils.wipePlayer(curPlayer);
}
if (data.isBurning(Metal.DURALUMIN) && !data.isEnhanced()) {
data.setEnhanced(2);
if (curPlayer instanceof ServerPlayer sp) {
Network.sync(new EnhanceTimePayload(2, sp.getId()), sp);

if (curPlayer instanceof ServerPlayer player) {
// Run the necessary updates on the player's metals
boolean syncRequired = data.tickBurning();

/*********************************************
* ALUMINUM AND DURALUMIN *
*********************************************/
if (data.isBurning(Metal.ALUMINUM)) {
PowerUtils.wipePlayer(curPlayer);
syncRequired = true;
}
} else if (!data.isBurning(Metal.DURALUMIN) && data.isEnhanced()) {
data.decEnhanced();
if (!data.isEnhanced()) { //Enhancement ran out this tick
if (curPlayer instanceof ServerPlayer sp) {
Network.sync(new EnhanceTimePayload(false, sp.getId()), sp);
if (data.isBurning(Metal.DURALUMIN) && !data.isEnhanced()) {
data.setEnhanced(2);
Network.sync(new EnhanceTimePayload(2, player.getId()), player);
} else if (!data.isBurning(Metal.DURALUMIN) && data.isEnhanced()) {
data.decrementEnhanced();
if (!data.isEnhanced()) { //Enhancement ran out this tick
Network.sync(new EnhanceTimePayload(false, player.getId()), player);
data.drainMetals(Arrays.stream(Metal.values()).filter(data::isBurning).toArray(Metal[]::new));
syncRequired = true;
}
data.drainMetals(Arrays.stream(Metal.values()).filter(data::isBurning).toArray(Metal[]::new));
}
}


// Run the necessary updates on the player's metals
// Ran AFTER duralumin and aluminum to make sure they function correctly
if (curPlayer instanceof ServerPlayer player) {
data.tickBurning(player);
if (syncRequired) {
Network.syncAllomancerData(player);
}
}

/*********************************************
Expand All @@ -298,7 +299,7 @@ private static void playerPowerTick(Player curPlayer, Level level) {
/*********************************************
* GOLD AND ELECTRUM (enhanced) *
*********************************************/
if (data.isEnhanced() && data.isBurning(Metal.ELECTRUM) && data.getAmount(Metal.ELECTRUM) >= 9) {
if (data.isEnhanced() && data.isBurning(Metal.ELECTRUM) && data.getStored(Metal.ELECTRUM) >= 9) {
ResourceKey<Level> spawnDim = data.getSpawnDim();
BlockPos spawnLoc;

Expand All @@ -317,7 +318,7 @@ private static void playerPowerTick(Player curPlayer, Level level) {
data.drainMetals(Metal.ELECTRUM);


} else if (data.isEnhanced() && data.isBurning(Metal.GOLD) && data.getAmount(Metal.GOLD) >= 9) { // These should be mutually exclusive
} else if (data.isEnhanced() && data.isBurning(Metal.GOLD) && data.getStored(Metal.GOLD) >= 9) { // These should be mutually exclusive
ResourceKey<Level> deathDim = data.getDeathDim();
if (deathDim != null) {
PowerUtils.teleport(curPlayer, level, deathDim, data.getDeathLoc());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void render(ExtendedGui gui, GuiGraphics guiGraphics, float partialTick,
*/
for (Metal mt : Metal.values()) {
if (data.hasPower(mt)) {
int metalY = 9 - data.getAmount(mt);
int metalY = 9 - data.getStored(mt);
int i = mt.getIndex();
int offset = (i / 2) * 4; // Adding a gap between pairs
// Draw the bars first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void render(GuiGraphics guiGraphics, int mx, int my, float partialTicks)
gs += 0x19;
}

gs = (!data.hasPower(mt) || data.getAmount(mt) == 0) ? 0 : gs;
gs = (!data.hasPower(mt) || data.getStored(mt) == 0) ? 0 : gs;

int r = data.isBurning(mt) ? 0xFF : gs;
int g = gs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static void toggleBurn(Metal metal, IAllomancerData data) {
return;
}

if (data.getAmount(metal) > 0) {
if (data.getStored(metal) > 0) {
data.setBurning(metal, !data.isBurning(metal));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@

import com.legobmw99.allomancy.api.data.IAllomancerData;
import com.legobmw99.allomancy.api.enums.Metal;
import com.legobmw99.allomancy.network.Network;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.common.util.INBTSerializable;

import java.util.Arrays;

public class AllomancerData implements IAllomancerData, INBTSerializable<CompoundTag> {

public static final int MAX_STORAGE = 10;
private static final int[] MAX_BURN_TIME = {1800, 1800, 3600, 600, 1800, 1800, 2400, 1600, 100, 20, 300, 40, 1000, 10000, 3600, 160};

private final boolean[] allomantic_powers;
private final int[] burn_time;
private final int[] metal_amounts;
Expand Down Expand Up @@ -50,7 +47,7 @@ public AllomancerData() {
}


public void tickBurning(ServerPlayer player) {
public boolean tickBurning() {
boolean sync = false;
for (Metal metal : Metal.values()) {
if (this.isBurning(metal)) {
Expand All @@ -59,23 +56,21 @@ public void tickBurning(ServerPlayer player) {
this.setBurning(metal, false);
sync = true;
} else {
this.setBurnTime(metal, this.getBurnTime(metal) - 1);
if (this.getBurnTime(metal) <= 0) {
if (this.getAmount(metal) <= 0) {
this.burn_time[metal.getIndex()]--;

if (this.burn_time[metal.getIndex()] <= 0) {
if (this.getStored(metal) <= 0) {
this.setBurning(metal, false);
} else {
this.setAmount(metal, this.getAmount(metal) - 1);
this.decrementStored(metal);
}
sync = true;
this.setBurnTime(metal, MAX_BURN_TIME[metal.getIndex()]);
this.burn_time[metal.getIndex()] = MAX_BURN_TIME[metal.getIndex()];
}
}
}
}
if (sync) {
Network.syncAllomancerData(player);
}

return sync;
}


Expand Down Expand Up @@ -149,25 +144,28 @@ public void setBurning(Metal metal, boolean metalBurning) {
}


public int getAmount(Metal metal) {
public int getStored(Metal metal) {
return this.metal_amounts[metal.getIndex()];
}


public void setAmount(Metal metal, int amt) {
this.metal_amounts[metal.getIndex()] = amt;
public void incrementStored(Metal metal) {
this.metal_amounts[metal.getIndex()]++;
}

public void decrementStored(Metal metal) {
if (this.metal_amounts[metal.getIndex()] > 0) {
this.metal_amounts[metal.getIndex()]--;
}
}

public void drainMetals(Metal... metals) {
for (Metal mt : metals) {
this.metal_amounts[mt.getIndex()] = 0;
// So that they burn out next tick
this.burn_time[mt.getIndex()] = 1;
this.burn_time[mt.getIndex()] = MAX_BURN_TIME[mt.getIndex()];
this.setBurning(mt, false);
}
}


public int getDamageStored() {
return this.damage_stored;
}
Expand Down Expand Up @@ -228,27 +226,7 @@ public ResourceKey<Level> getSpawnDim() {
return ResourceKey.create(Registries.DIMENSION, new ResourceLocation(this.spawn_dimension));
}

/**
* Get the burn time of a specific metal
*
* @param metal the metal to retrieve
* @return the burn time
*/
protected int getBurnTime(Metal metal) {
return this.burn_time[metal.getIndex()];
}

/**
* Set the burn time of a specific metal
*
* @param metal the metal to set
* @param burnTime the burn time
*/
protected void setBurnTime(Metal metal, int burnTime) {
this.burn_time[metal.getIndex()] = burnTime;
}

public void decEnhanced() {
public void decrementEnhanced() {
if (isEnhanced()) {
this.enhanced_time--;
}
Expand Down Expand Up @@ -276,7 +254,7 @@ public CompoundTag serializeNBT() {

CompoundTag metal_storage = new CompoundTag();
for (Metal mt : Metal.values()) {
metal_storage.putInt(mt.getName(), this.getAmount(mt));
metal_storage.putInt(mt.getName(), this.getStored(mt));
}
allomancy_data.put("metal_storage", metal_storage);

Expand Down Expand Up @@ -320,7 +298,7 @@ public void deserializeNBT(CompoundTag allomancy_data) {

CompoundTag metal_storage = (CompoundTag) allomancy_data.get("metal_storage");
for (Metal mt : Metal.values()) {
this.setAmount(mt, metal_storage.getInt(mt.getName()));
this.metal_amounts[mt.getIndex()] = metal_storage.getInt(mt.getName());
}

CompoundTag metal_burning = (CompoundTag) allomancy_data.get("metal_burning");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static void toggleBurnRequest(final ToggleBurnPayload payload, final Play

boolean value = payload.on();

if (!data.hasPower(mt) || data.getAmount(mt) <= 0) {
if (!data.hasPower(mt) || data.getStored(mt) <= 0) {
value = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

public class AllomancyConfig {

public static final ModConfigSpec COMMON_CONFIG;
public static final ModConfigSpec CLIENT_CONFIG;
private static final ModConfigSpec COMMON_CONFIG;
private static final ModConfigSpec CLIENT_CONFIG;
public static final ModConfigSpec SERVER_CONFIG;


Expand Down

0 comments on commit 0d3400a

Please sign in to comment.