Skip to content

Commit

Permalink
IT COMPILES!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellpeck committed Sep 26, 2024
1 parent e50d3fc commit 32baa11
Show file tree
Hide file tree
Showing 23 changed files with 311 additions and 247 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ tasks.withType(ProcessResources).configureEach {
loader_version_range: loader_version_range,
mod_id : mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: mod_version,
mod_authors : mod_authors, mod_description: mod_description,
patchouli_version : patchouli_version
]
inputs.properties replaceProperties

Expand Down
17 changes: 14 additions & 3 deletions src/main/java/de/ellpeck/naturesaura/Helper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.ellpeck.naturesaura;

import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.ellpeck.naturesaura.api.aura.container.IAuraContainer;
import de.ellpeck.naturesaura.api.aura.item.IAuraRecharge;
import de.ellpeck.naturesaura.api.misc.ILevelData;
Expand All @@ -13,6 +15,7 @@
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.IntArrayTag;
import net.minecraft.nbt.Tag;
Expand Down Expand Up @@ -356,14 +359,13 @@ public static void mineRecursively(Level level, BlockPos pos, BlockPos start, It
}

public static boolean isToolEnabled(ItemStack stack) {
return stack.hasTag() && !stack.getTag().getBoolean(NaturesAura.MOD_ID + ":disabled");
return stack.has(DisableableToolData.TYPE) && !stack.get(DisableableToolData.TYPE).disabled;
}

public static boolean toggleToolEnabled(Player player, ItemStack stack) {
if (!player.isShiftKeyDown())
return false;
var disabled = !Helper.isToolEnabled(stack);
stack.getOrCreateTag().putBoolean(NaturesAura.MOD_ID + ":disabled", !disabled);
stack.set(DisableableToolData.TYPE, new DisableableToolData(Helper.isToolEnabled(stack)));
player.level().playSound(null, player.getX() + 0.5, player.getY() + 0.5, player.getZ() + 0.5, SoundEvents.ARROW_HIT_PLAYER, SoundSource.PLAYERS, 0.65F, 1F);
return true;
}
Expand All @@ -377,4 +379,13 @@ public static BlockPos readBlockPos(Tag tag) {
return null;
}

public record DisableableToolData(boolean disabled) {

public static final Codec<DisableableToolData> CODEC = RecordCodecBuilder.create(i -> i.group(
Codec.BOOL.fieldOf("disabled").forGetter(d -> d.disabled)
).apply(i, DisableableToolData::new));
public static final DataComponentType<DisableableToolData> TYPE = DataComponentType.<DisableableToolData>builder().persistent(DisableableToolData.CODEC).cacheEncoding().build();

}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package de.ellpeck.naturesaura.api.aura.container;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.world.item.ItemStack;

public class ItemAuraContainer implements IAuraContainer {
Expand Down Expand Up @@ -37,16 +39,13 @@ public int drainAura(int amountToDrain, boolean simulate) {
}

private void setAura(int amount) {
if (!this.stack.hasTag()) {
this.stack.setTag(new CompoundTag());
}
this.stack.getTag().putInt("aura", amount);
this.stack.set(Data.TYPE, new Data(amount));
}

@Override
public int getStoredAura() {
if (this.stack.hasTag()) {
return this.stack.getTag().getInt("aura");
if (this.stack.has(Data.TYPE)) {
return this.stack.get(Data.TYPE).auraAmount;
} else {
return 0;
}
Expand All @@ -66,4 +65,14 @@ public int getAuraColor() {
public boolean isAcceptableType(IAuraType type) {
return this.type == null || this.type == type;
}

public record Data(int auraAmount) {

public static final Codec<Data> CODEC = RecordCodecBuilder.create(i -> i.group(
Codec.INT.fieldOf("aura_amount").forGetter(d -> d.auraAmount)
).apply(i, Data::new));
public static final DataComponentType<Data> TYPE = DataComponentType.<Data>builder().persistent(Data.CODEC).cacheEncoding().build();

}

}
24 changes: 16 additions & 8 deletions src/main/java/de/ellpeck/naturesaura/blocks/BlockEnderCrate.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.ellpeck.naturesaura.blocks;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.misc.ILevelData;
import de.ellpeck.naturesaura.blocks.tiles.BlockEntityEnderCrate;
Expand All @@ -13,29 +15,26 @@
import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.core.BlockPos;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.AnvilUpdateEvent;
import net.neoforged.bus.api.SubscribeEvent;

import javax.annotation.Nullable;
import java.util.List;

public class BlockEnderCrate extends BlockContainerImpl implements ITESRProvider<BlockEntityEnderCrate>, ICustomBlockState {
Expand All @@ -47,9 +46,9 @@ public BlockEnderCrate() {
}

public static String getEnderName(ItemStack stack) {
if (!stack.hasTag())
if (!stack.has(Data.TYPE))
return "";
return stack.getTag().getString(NaturesAura.MOD_ID + ":ender_name");
return stack.get(Data.TYPE).enderName;
}

@OnlyIn(Dist.CLIENT)
Expand Down Expand Up @@ -79,7 +78,7 @@ public void onAnvilUpdate(AnvilUpdateEvent event) {
if (ILevelData.getOverworldData(player.level()).isEnderStorageLocked(name))
return;
var output = stack.copy();
output.getOrCreateTag().putString(NaturesAura.MOD_ID + ":ender_name", name);
output.set(Data.TYPE, new Data(name));
event.setOutput(output);
event.setMaterialCost(stack.getCount());
event.setCost(1);
Expand Down Expand Up @@ -131,4 +130,13 @@ public void registerTESR() {
BlockEntityRenderers.register(ModBlockEntities.ENDER_CRATE, RenderEnderCrate::new);
}

public record Data(String enderName) {

public static final Codec<Data> CODEC = RecordCodecBuilder.create(i -> i.group(
Codec.STRING.fieldOf("name").forGetter(d -> d.enderName)
).apply(i, Data::new));
public static final DataComponentType<Data> TYPE = DataComponentType.<Data>builder().persistent(Data.CODEC).cacheEncoding().build();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,8 @@ public void dropInventory() {

@Override
public void modifyDrop(ItemStack regularItem) {
if (this.name != null) {
if (!regularItem.hasTag())
regularItem.setTag(new CompoundTag());
regularItem.getTag().putString(NaturesAura.MOD_ID + ":ender_name", this.name);
}
if (this.name != null)
regularItem.set(BlockEnderCrate.Data.TYPE, new BlockEnderCrate.Data(this.name));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.core.component.DataComponents;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
Expand Down Expand Up @@ -57,35 +57,33 @@ public void tick() {
}

if (this.trackedEntity != null && !this.trackedEntity.isAlive()) {
if (this.trackedItem.hasTag()) {
if (this.trackedItem.has(DataComponents.FIREWORKS)) {
float generateFactor = 0;
Set<Integer> usedColors = new HashSet<>();

var compound = this.trackedItem.getTag();
var fireworks = compound.getCompound("Fireworks");
var fireworks = this.trackedItem.get(DataComponents.FIREWORKS);

var flightTime = fireworks.getInt("Flight");
var explosions = fireworks.getList("Explosions", 10);
var flightTime = fireworks.flightDuration();
var explosions = fireworks.explosions();
if (!explosions.isEmpty()) {
generateFactor += flightTime;

for (var base : explosions) {
var explosion = (CompoundTag) base;
for (var explosion : explosions) {
generateFactor += 1.5F;

var flicker = explosion.getBoolean("Flicker");
var flicker = explosion.hasTwinkle();
if (flicker)
generateFactor += 1;

var trail = explosion.getBoolean("Trail");
var trail = explosion.hasTrail();
if (trail)
generateFactor += 8;

var type = explosion.getByte("Type");
generateFactor += new float[]{0, 1, 0.5F, 20, 0.5F}[type];
var type = explosion.shape();
generateFactor += new float[]{0, 1, 0.5F, 20, 0.5F}[type.getId()];

Set<Integer> colors = new HashSet<>();
for (var color : explosion.getIntArray("Colors")) {
for (var color : explosion.colors()) {
usedColors.add(color);
colors.add(color);
}
Expand All @@ -106,8 +104,8 @@ public void tick() {
data.add(this.worldPosition.getZ());
data.addAll(usedColors);
PacketHandler.sendToAllLoaded(this.level, this.worldPosition, new PacketParticles(
(float) this.trackedEntity.getX(), (float) this.trackedEntity.getY(), (float) this.trackedEntity.getZ(),
PacketParticles.Type.FIREWORK_GEN, Ints.toArray(data)));
(float) this.trackedEntity.getX(), (float) this.trackedEntity.getY(), (float) this.trackedEntity.getZ(),
PacketParticles.Type.FIREWORK_GEN, Ints.toArray(data)));
}
}

Expand All @@ -122,7 +120,7 @@ public void tick() {
this.toRelease = 0;

PacketHandler.sendToAllLoaded(this.level, this.worldPosition,
new PacketParticles(this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ(), PacketParticles.Type.FLOWER_GEN_AURA_CREATION));
new PacketParticles(this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ(), PacketParticles.Type.FLOWER_GEN_AURA_CREATION));
}
}
}
Expand All @@ -132,4 +130,5 @@ public void tick() {
public boolean wantsLimitRemover() {
return true;
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package de.ellpeck.naturesaura.blocks.tiles;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.blocks.ModBlocks;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
Expand Down Expand Up @@ -103,16 +106,13 @@ public void dropInventory() {
public void modifyDrop(ItemStack regularItem) {
var compound = new CompoundTag();
this.writeNBT(compound, SaveType.BLOCK, this.getLevel().registryAccess());
if (!compound.isEmpty()) {
if (!regularItem.hasTag())
regularItem.setTag(new CompoundTag());
regularItem.getTag().put("data", compound);
}
if (!compound.isEmpty())
regularItem.set(DroppedItemData.TYPE, new DroppedItemData(compound));
}

public void loadDataOnPlace(ItemStack stack) {
if (stack.hasTag()) {
var compound = stack.getTag().getCompound("data");
if (stack.has(DroppedItemData.TYPE)) {
var compound = stack.get(DroppedItemData.TYPE).data;
if (compound != null)
this.readNBT(compound, SaveType.BLOCK, this.level.registryAccess());
}
Expand Down Expand Up @@ -161,4 +161,13 @@ public enum SaveType {
TILE, SYNC, BLOCK
}

public record DroppedItemData(CompoundTag data) {

public static final Codec<DroppedItemData> CODEC = RecordCodecBuilder.create(i -> i.group(
CompoundTag.CODEC.fieldOf("data").forGetter(d -> d.data)
).apply(i, DroppedItemData::new));
public static final DataComponentType<DroppedItemData> TYPE = DataComponentType.<DroppedItemData>builder().persistent(DroppedItemData.CODEC).cacheEncoding().build();

}

}
15 changes: 8 additions & 7 deletions src/main/java/de/ellpeck/naturesaura/chunk/AuraChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import de.ellpeck.naturesaura.packet.PacketAuraChunk;
import de.ellpeck.naturesaura.packet.PacketHandler;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -168,15 +169,15 @@ public void update() {
if (this.needsSync) {
var pos = this.chunk.getPos();
PacketHandler.sendToAllLoaded(level,
new BlockPos(pos.x * 16, 0, pos.z * 16),
this.makePacket());
new BlockPos(pos.x * 16, 0, pos.z * 16),
this.makePacket());
this.needsSync = false;
}
}

public PacketAuraChunk makePacket() {
var pos = this.chunk.getPos();
return new PacketAuraChunk(pos.x, pos.z, this.drainSpots.values());
return new PacketAuraChunk(pos.x, pos.z, this.drainSpots.values().stream().map(DrainSpot::serializeNBT).toList());
}

public void getSpots(BlockPos pos, int radius, BiConsumer<BlockPos, Integer> consumer) {
Expand Down Expand Up @@ -222,8 +223,8 @@ public Pair<BlockPos, Integer>[] getLowestAndHighestSpots(BlockPos pos, int radi
}
});
ret = new Pair[]{
Pair.of(lowestSpot.getValue(), lowestAmount.intValue()),
Pair.of(highestSpot.getValue(), highestAmount.intValue())};
Pair.of(lowestSpot.getValue(), lowestAmount.intValue()),
Pair.of(highestSpot.getValue(), highestAmount.intValue())};
this.limitSpotCache.put(pos, radius, ret);
}
return ret;
Expand All @@ -249,7 +250,7 @@ public void getActiveEffectIcons(Player player, Map<ResourceLocation, Tuple<Item
}

@Override
public CompoundTag serializeNBT() {
public CompoundTag serializeNBT(HolderLookup.Provider registries) {
var list = new ListTag();
for (var spot : this.drainSpots.values())
list.add(spot.serializeNBT());
Expand All @@ -259,7 +260,7 @@ public CompoundTag serializeNBT() {
}

@Override
public void deserializeNBT(CompoundTag compound) {
public void deserializeNBT(HolderLookup.Provider registries, CompoundTag compound) {
this.drainSpots.clear();
var list = compound.getList("drain_spots", 10);
for (var base : list)
Expand Down
Loading

0 comments on commit 32baa11

Please sign in to comment.