ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFireworkBox.java

322 lines
11 KiB
Java
Raw Normal View History

2015-12-13 17:32:06 +01:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityFireworkBox.java") is part of the Actually Additions mod for Minecraft.
2015-12-13 17:32:06 +01:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-12-13 17:32:06 +01:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-12-13 17:32:06 +01:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-12-13 17:32:06 +01:00
2021-11-13 18:16:25 +01:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerFireworkBox;
import de.ellpeck.actuallyadditions.mod.network.gui.INumberReactor;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.minecraft.util.random.Weight;
import net.minecraft.util.random.WeightedEntry;
import net.minecraft.util.random.WeightedRandom;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.FireworkRocketEntity;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
2024-03-04 20:21:48 +01:00
import net.neoforged.neoforge.energy.IEnergyStorage;
2015-12-13 17:32:06 +01:00
import javax.annotation.Nullable;
2021-02-26 22:15:48 +01:00
import java.util.ArrayList;
import java.util.List;
2024-03-02 21:23:08 +01:00
public class TileEntityFireworkBox extends TileEntityBase implements IEnergyDisplay, INumberReactor, MenuProvider {
public static final int USE_PER_SHOT = 500;
2016-11-26 20:43:50 +01:00
public final CustomEnergyStorage storage = new CustomEnergyStorage(20000, 200, 0);
public int intValuePlay = 2;
public int chargeAmount = 2;
public int flightTime = 2;
public float trailOrFlickerChance = 0.65F;
public float flickerChance = 0.25F;
public int colorAmount = 3;
public float typeChance0 = 1F;
public float typeChance1 = 0F;
public float typeChance2 = 0F;
public float typeChance3 = 0F;
public float typeChance4 = 0F;
public int areaOfEffect = 2;
2017-02-04 16:48:22 +01:00
private int timeUntilNextFirework;
private int oldEnergy;
2024-03-02 21:23:08 +01:00
public TileEntityFireworkBox(BlockPos pos, BlockState state) {
super(ActuallyBlocks.FIREWORK_BOX.getTileEntityType(), pos, state);
}
@Override
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
super.writeSyncableNBT(compound, type);
this.storage.writeToNBT(compound);
2019-05-02 09:10:29 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
compound.putInt("Play", this.intValuePlay);
compound.putInt("ChargeAmount", this.chargeAmount);
compound.putInt("FlightTime", this.flightTime);
compound.putFloat("TrailFlickerChance", this.trailOrFlickerChance);
compound.putFloat("FlickerChance", this.flickerChance);
compound.putInt("ColorAmount", this.colorAmount);
compound.putFloat("TypeChance0", this.typeChance0);
compound.putFloat("TypeChance1", this.typeChance1);
compound.putFloat("TypeChance2", this.typeChance2);
compound.putFloat("TypeChance3", this.typeChance3);
compound.putFloat("TypeChance4", this.typeChance4);
compound.putInt("Area", this.areaOfEffect);
}
}
@Override
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
super.readSyncableNBT(compound, type);
this.storage.readFromNBT(compound);
2019-05-02 09:10:29 +02:00
if (type != NBTType.SAVE_BLOCK) {
2021-02-27 16:33:00 +01:00
this.intValuePlay = compound.getInt("Play");
this.chargeAmount = compound.getInt("ChargeAmount");
this.flightTime = compound.getInt("FlightTime");
this.trailOrFlickerChance = compound.getFloat("TrailFlickerChance");
this.flickerChance = compound.getFloat("FlickerChance");
2021-02-27 16:33:00 +01:00
this.colorAmount = compound.getInt("ColorAmount");
this.typeChance0 = compound.getFloat("TypeChance0");
this.typeChance1 = compound.getFloat("TypeChance1");
this.typeChance2 = compound.getFloat("TypeChance2");
this.typeChance3 = compound.getFloat("TypeChance3");
this.typeChance4 = compound.getFloat("TypeChance4");
2021-02-27 16:33:00 +01:00
this.areaOfEffect = compound.getInt("Area");
}
}
@Override
2024-03-02 21:23:08 +01:00
public void onNumberReceived(double number, int id, Player player) {
2019-05-02 09:10:29 +02:00
switch (id) {
2021-02-26 22:15:48 +01:00
case 0:
this.intValuePlay = (int) number;
break;
case 1:
this.chargeAmount = (int) number;
break;
case 2:
this.flightTime = (int) number;
break;
case 3:
this.trailOrFlickerChance = (float) number;
break;
case 4:
this.flickerChance = (float) number;
break;
case 5:
this.colorAmount = (int) number;
break;
case 6:
this.typeChance0 = (float) number;
break;
case 7:
this.typeChance1 = (float) number;
break;
case 8:
this.typeChance2 = (float) number;
break;
case 9:
this.typeChance3 = (float) number;
break;
case 10:
this.typeChance4 = (float) number;
break;
case 11:
this.areaOfEffect = (int) number;
break;
}
2024-03-10 01:52:32 +01:00
this.setChanged();
this.sendUpdate();
}
2024-03-02 21:23:08 +01:00
public void spawnFireworks(Level world, double x, double y, double z) {
ItemStack firework = this.makeFirework();
2016-05-16 17:00:29 +02:00
2019-05-02 09:10:29 +02:00
double newX = x + this.getRandomAoe();
double newZ = z + this.getRandomAoe();
2024-03-03 01:20:53 +01:00
if (world.hasChunkAt(BlockPos.containing(newX, y, newZ))) {
2021-02-27 16:33:00 +01:00
FireworkRocketEntity rocket = new FireworkRocketEntity(world, newX, y + 1, newZ, firework);
world.addFreshEntity(rocket);
}
}
2019-05-02 09:10:29 +02:00
private double getRandomAoe() {
if (this.areaOfEffect <= 0) {
return 0.5;
2019-05-02 09:10:29 +02:00
} else {
2024-03-02 21:23:08 +01:00
return Mth.nextDouble(this.level.random, 0, this.areaOfEffect * 2) - this.areaOfEffect;
2016-05-16 17:00:29 +02:00
}
}
2019-05-02 09:10:29 +02:00
private ItemStack makeFirework() {
2024-03-02 21:23:08 +01:00
ListTag list = new ListTag();
2019-05-02 09:10:29 +02:00
for (int i = 0; i < this.getRandomWithPlay(this.chargeAmount); i++) {
2021-02-27 16:33:00 +01:00
list.add(this.makeFireworkCharge());
2015-12-13 17:32:06 +01:00
}
2024-03-02 21:23:08 +01:00
CompoundTag compound1 = new CompoundTag();
2021-02-27 16:33:00 +01:00
compound1.put("Explosions", list);
compound1.putByte("Flight", (byte) this.getRandomWithPlay(this.flightTime));
2015-12-13 17:32:06 +01:00
2024-03-02 21:23:08 +01:00
CompoundTag compound = new CompoundTag();
2021-02-27 16:33:00 +01:00
compound.put("Fireworks", compound1);
2015-12-13 17:32:06 +01:00
2021-02-27 16:33:00 +01:00
ItemStack firework = new ItemStack(Items.FIREWORK_ROCKET);
firework.setTag(compound);
2015-12-13 17:32:06 +01:00
return firework;
}
2024-03-02 21:23:08 +01:00
private CompoundTag makeFireworkCharge() {
CompoundTag compound = new CompoundTag();
2015-12-13 17:32:06 +01:00
if (this.level.random.nextFloat() <= this.trailOrFlickerChance) {
if (this.level.random.nextFloat() <= this.flickerChance) {
2021-02-27 16:33:00 +01:00
compound.putBoolean("Flicker", true);
2019-05-02 09:10:29 +02:00
} else {
2021-02-27 16:33:00 +01:00
compound.putBoolean("Trail", true);
2015-12-13 17:32:06 +01:00
}
}
2021-02-27 16:33:00 +01:00
// TODO: [port] Validate this is the correct way to get colors
int[] colors = new int[this.getRandomWithPlay(this.colorAmount)];
2019-05-02 09:10:29 +02:00
for (int i = 0; i < colors.length; i++) {
2024-03-02 21:23:08 +01:00
colors[i] = DyeColor.values()[this.level.random.nextInt(DyeColor.values().length)].getFireworkColor();
2015-12-13 17:32:06 +01:00
}
2021-02-27 16:33:00 +01:00
compound.putIntArray("Colors", colors);
2015-12-13 17:32:06 +01:00
2021-02-27 16:33:00 +01:00
compound.putByte("Type", (byte) this.getRandomType());
2015-12-13 17:32:06 +01:00
return compound;
}
2019-05-02 09:10:29 +02:00
private int getRandomWithPlay(int value) {
2024-03-02 21:23:08 +01:00
return Mth.clamp(Mth.nextInt(this.level.random, value - this.intValuePlay, value + this.intValuePlay), 1, 6);
2016-06-01 00:39:35 +02:00
}
2019-05-02 09:10:29 +02:00
private int getRandomType() {
2019-02-27 19:53:05 +01:00
List<WeightedFireworkType> possible = new ArrayList<>();
possible.add(new WeightedFireworkType(0, this.typeChance0));
possible.add(new WeightedFireworkType(1, this.typeChance1));
possible.add(new WeightedFireworkType(2, this.typeChance2));
possible.add(new WeightedFireworkType(3, this.typeChance3));
possible.add(new WeightedFireworkType(4, this.typeChance4));
int weight = WeightedRandom.getTotalWeight(possible);
2019-05-02 09:10:29 +02:00
if (weight <= 0) {
return 0;
2019-05-02 09:10:29 +02:00
} else {
2024-03-02 21:23:08 +01:00
return WeightedRandom.getRandomItem(this.level.random, possible, weight).map(weightedFireworkType -> weightedFireworkType.type).orElse(0);
}
2016-06-01 00:39:35 +02:00
}
2024-03-02 21:23:08 +01:00
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityFireworkBox tile) {
tile.clientTick();
}
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityFireworkBox tile) {
tile.serverTick();
if (!tile.isRedstonePowered && !tile.isPulseMode) {
if (tile.timeUntilNextFirework > 0) {
tile.timeUntilNextFirework--;
if (tile.timeUntilNextFirework <= 0) {
tile.doWork();
2016-06-01 00:39:35 +02:00
}
2019-05-02 09:10:29 +02:00
} else {
2024-03-02 21:23:08 +01:00
tile.timeUntilNextFirework = 100;
2016-06-01 00:39:35 +02:00
}
}
2024-03-02 21:23:08 +01:00
if (tile.oldEnergy != tile.storage.getEnergyStored() && tile.sendUpdateWithInterval()) {
tile.oldEnergy = tile.storage.getEnergyStored();
2016-06-01 00:39:35 +02:00
}
}
}
2019-05-02 09:10:29 +02:00
private void doWork() {
if (this.storage.getEnergyStored() >= USE_PER_SHOT) {
this.spawnFireworks(this.level, this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ());
2016-06-01 00:39:35 +02:00
this.storage.extractEnergyInternal(USE_PER_SHOT, false);
2016-06-01 00:39:35 +02:00
}
}
@Override
2019-05-02 09:10:29 +02:00
public boolean isRedstoneToggle() {
return true;
}
@Override
2019-05-02 09:10:29 +02:00
public void activateOnPulse() {
this.doWork();
}
@Override
2019-05-02 09:10:29 +02:00
public CustomEnergyStorage getEnergyStorage() {
return this.storage;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean needsHoldShift() {
return false;
}
2016-11-26 08:58:42 +01:00
@Override
2024-03-04 20:21:48 +01:00
public IEnergyStorage getEnergyStorage(Direction facing) {
return this.storage;
2016-11-26 08:58:42 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public Component getDisplayName() {
return Component.translatable("container.actuallyadditions.fireworkBox");
}
@Nullable
@Override
2024-03-02 21:23:08 +01:00
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player p_createMenu_3_) {
2024-03-10 01:52:32 +01:00
return new ContainerFireworkBox(windowId, playerInventory, this);
}
2024-03-02 21:23:08 +01:00
private static class WeightedFireworkType implements WeightedEntry {
public final int type;
2024-03-02 21:23:08 +01:00
public final Weight chance;
2019-05-02 09:10:29 +02:00
public WeightedFireworkType(int type, float chance) {
this.type = type;
2024-03-02 21:23:08 +01:00
this.chance = Weight.of((int) (chance * 100F));
}
@Override
public Weight getWeight() {
return this.chance;
}
}
2015-12-13 17:32:06 +01:00
}