fixed the server and brought back the chunk loader!

This commit is contained in:
Ellpeck 2020-01-24 20:49:09 +01:00
parent 3ff0aed4ad
commit 932d4ed039
14 changed files with 532 additions and 516 deletions

View file

@ -63,7 +63,7 @@ public final class Helper {
if (chunk != null) { if (chunk != null) {
for (BlockPos tilePos : chunk.getTileEntitiesPos()) { for (BlockPos tilePos : chunk.getTileEntitiesPos()) {
if (tilePos.distanceSq(pos) <= radius * radius) if (tilePos.distanceSq(pos) <= radius * radius)
if (consumer.apply(world.getTileEntity(tilePos))) if (consumer.apply(chunk.getTileEntity(tilePos)))
return true; return true;
} }
} }

View file

@ -27,14 +27,6 @@ public class BlockChunkLoader extends BlockContainerImpl implements IVisualizabl
super("chunk_loader", TileEntityChunkLoader::new, ModBlocks.prop(Material.ROCK).hardnessAndResistance(3F).sound(SoundType.STONE)); super("chunk_loader", TileEntityChunkLoader::new, ModBlocks.prop(Material.ROCK).hardnessAndResistance(3F).sound(SoundType.STONE));
} }
/* TODO Chunk Loading
@Override
public void onInit(FMLInitializationEvent event) {
super.onInit(event);
ForgeChunkManager.setForcedChunkLoadingCallback(NaturesAura.instance, new ChunkLoadingCallback());
}
*/
@Override @Override
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public AxisAlignedBB getVisualizationBounds(World world, BlockPos pos) { public AxisAlignedBB getVisualizationBounds(World world, BlockPos pos) {
@ -79,21 +71,4 @@ public class BlockChunkLoader extends BlockContainerImpl implements IVisualizabl
return SHAPE; return SHAPE;
} }
// TODO chunk loading
/*public static class ChunkLoadingCallback implements ForgeChunkManager.LoadingCallback {
@Override
public void ticketsLoaded(List<Ticket> tickets, World world) {
for (Ticket ticket : tickets) {
CompoundNBT data = ticket.getModData();
BlockPos pos = BlockPos.fromLong(data.getLong("pos"));
TileEntity tile = world.getTileEntity(pos);
if (!(tile instanceof TileEntityChunkLoader))
continue;
TileEntityChunkLoader loader = (TileEntityChunkLoader) tile;
loader.updateTicket(ticket);
loader.loadChunks();
}
}
}*/
} }

View file

@ -1,73 +1,84 @@
package de.ellpeck.naturesaura.blocks.tiles; package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk; import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity; import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
import net.minecraft.world.server.ServerWorld;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
// TODO chunk loader
public class TileEntityChunkLoader extends TileEntityImpl implements ITickableTileEntity { public class TileEntityChunkLoader extends TileEntityImpl implements ITickableTileEntity {
private final List<ChunkPos> forcedChunks = new ArrayList<>();
public TileEntityChunkLoader() { public TileEntityChunkLoader() {
super(ModTileEntities.CHUNK_LOADER); super(ModTileEntities.CHUNK_LOADER);
} }
//private Ticket ticket;
/*
@Override @Override
public void validate() { public void validate() {
super.validate(); super.validate();
if (!this.world.isRemote && this.ticket == null) { this.loadChunks(false);
Ticket ticket = ForgeChunkManager.requestTicket(NaturesAura.instance, this.world, Type.NORMAL);
this.updateTicket(ticket);
ticket.getModData().setLong("pos", this.pos.toLong());
}
} }
@Override @Override
public void invalidate() { public void remove() {
super.invalidate(); super.remove();
if (!this.world.isRemote) this.loadChunks(true);
this.updateTicket(null);
} }
@Override @Override
public void onRedstonePowerChange(int newPower) { public void onRedstonePowerChange(int newPower) {
super.onRedstonePowerChange(newPower); super.onRedstonePowerChange(newPower);
if (!this.world.isRemote) { if (!this.world.isRemote) {
this.loadChunks(); this.loadChunks(false);
this.sendToClients(); this.sendToClients();
} }
}*/ }
public int range() { public int range() {
return this.redstonePower * 2; return this.redstonePower * 2;
} }
/*public void updateTicket(Ticket ticket) { private void loadChunks(boolean unload) {
if (this.ticket != null) if (this.world.isRemote)
ForgeChunkManager.releaseTicket(this.ticket);
this.ticket = ticket;
}
public void loadChunks() {
if (this.ticket == null)
return; return;
Set<ChunkPos> before = new HashSet<>(this.ticket.getChunkList()); ServerWorld world = (ServerWorld) this.world;
List<ChunkPos> shouldBeForced = new ArrayList<>();
if (!unload) {
int range = this.range(); int range = this.range();
if (range > 0) { if (range > 0) {
for (int x = (this.pos.getX() - range) >> 4; x <= (this.pos.getX() + range) >> 4; x++) { for (int x = (this.pos.getX() - range) >> 4; x <= (this.pos.getX() + range) >> 4; x++) {
for (int z = (this.pos.getZ() - range) >> 4; z <= (this.pos.getZ() + range) >> 4; z++) { for (int z = (this.pos.getZ() - range) >> 4; z <= (this.pos.getZ() + range) >> 4; z++) {
ChunkPos pos = new ChunkPos(x, z); ChunkPos pos = new ChunkPos(x, z);
if (!before.contains(pos)) // Only force chunks that we're already forcing or that nobody else is forcing
ForgeChunkManager.forceChunk(this.ticket, pos); if (this.forcedChunks.contains(pos) || !world.getForcedChunks().contains(pos.asLong()))
else shouldBeForced.add(pos);
before.remove(pos);
} }
} }
} }
for (ChunkPos pos : before) }
ForgeChunkManager.unforceChunk(this.ticket, pos);
}*/ // Unforce all of the chunks that shouldn't be forced anymore
for (ChunkPos pos : this.forcedChunks) {
if (!shouldBeForced.contains(pos))
world.forceChunk(pos.x, pos.z, false);
}
this.forcedChunks.clear();
// Force all chunks that should be forced
for (ChunkPos pos : shouldBeForced) {
world.forceChunk(pos.x, pos.z, true);
this.forcedChunks.add(pos);
}
}
@Override @Override
public void tick() { public void tick() {
@ -81,4 +92,21 @@ public class TileEntityChunkLoader extends TileEntityImpl implements ITickableTi
} }
} }
} }
@Override
public void writeNBT(CompoundNBT compound, SaveType type) {
super.writeNBT(compound, type);
if (type == SaveType.TILE)
compound.putLongArray("forced_chunks", this.forcedChunks.stream().map(ChunkPos::asLong).collect(Collectors.toList()));
}
@Override
public void readNBT(CompoundNBT compound, SaveType type) {
super.readNBT(compound, type);
if (type == SaveType.TILE) {
this.forcedChunks.clear();
Arrays.stream(compound.getLongArray("forced_chunks")).mapToObj(ChunkPos::new).forEach(this.forcedChunks::add);
}
}
} }

View file

@ -31,7 +31,7 @@ public class AuraBottle extends ItemImpl implements IColorProvidingItem {
public AuraBottle(Item emptyBottle) { public AuraBottle(Item emptyBottle) {
super("aura_bottle", new Properties().group(NaturesAura.CREATIVE_TAB)); super("aura_bottle", new Properties().group(NaturesAura.CREATIVE_TAB));
MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.register(new EventHandler());
DispenserBlock.registerDispenseBehavior(emptyBottle, (source, stack) -> { DispenserBlock.registerDispenseBehavior(emptyBottle, (source, stack) -> {
World world = source.getWorld(); World world = source.getWorld();
@ -55,34 +55,6 @@ public class AuraBottle extends ItemImpl implements IColorProvidingItem {
}); });
} }
@SubscribeEvent
public void onRightClick(PlayerInteractEvent.RightClickItem event) {
ItemStack held = event.getItemStack();
if (held.isEmpty() || held.getItem() != ModItems.BOTTLE_TWO_THE_REBOTTLING)
return;
PlayerEntity player = event.getPlayer();
RayTraceResult ray = rayTrace(player.world, player, RayTraceContext.FluidMode.NONE);
if (ray.getType() == RayTraceResult.Type.BLOCK)
return;
BlockPos pos = player.getPosition();
if (IAuraChunk.getAuraInArea(player.world, pos, 30) < 100000)
return;
if (!player.world.isRemote) {
held.shrink(1);
player.inventory.addItemStackToInventory(
setType(new ItemStack(this), IAuraType.forWorld(player.world)));
BlockPos spot = IAuraChunk.getHighestSpot(player.world, pos, 30, pos);
IAuraChunk.getAuraChunk(player.world, spot).drainAura(spot, 20000);
player.world.playSound(null, player.posX, player.posY, player.posZ,
SoundEvents.ITEM_BOTTLE_FILL_DRAGONBREATH, SoundCategory.PLAYERS, 1F, 1F);
}
player.swingArm(event.getHand());
}
@Override @Override
public void fillItemGroup(ItemGroup tab, NonNullList<ItemStack> items) { public void fillItemGroup(ItemGroup tab, NonNullList<ItemStack> items) {
if (this.isInGroup(tab)) { if (this.isInGroup(tab)) {
@ -118,4 +90,36 @@ public class AuraBottle extends ItemImpl implements IColorProvidingItem {
public IItemColor getItemColor() { public IItemColor getItemColor() {
return (stack, tintIndex) -> tintIndex > 0 ? getType(stack).getColor() : 0xFFFFFF; return (stack, tintIndex) -> tintIndex > 0 ? getType(stack).getColor() : 0xFFFFFF;
} }
private class EventHandler {
@SubscribeEvent
public void onRightClick(PlayerInteractEvent.RightClickItem event) {
ItemStack held = event.getItemStack();
if (held.isEmpty() || held.getItem() != ModItems.BOTTLE_TWO_THE_REBOTTLING)
return;
PlayerEntity player = event.getPlayer();
RayTraceResult ray = rayTrace(player.world, player, RayTraceContext.FluidMode.NONE);
if (ray.getType() == RayTraceResult.Type.BLOCK)
return;
BlockPos pos = player.getPosition();
if (IAuraChunk.getAuraInArea(player.world, pos, 30) < 100000)
return;
if (!player.world.isRemote) {
held.shrink(1);
player.inventory.addItemStackToInventory(
setType(new ItemStack(AuraBottle.this), IAuraType.forWorld(player.world)));
BlockPos spot = IAuraChunk.getHighestSpot(player.world, pos, 30, pos);
IAuraChunk.getAuraChunk(player.world, spot).drainAura(spot, 20000);
player.world.playSound(null, player.posX, player.posY, player.posZ,
SoundEvents.ITEM_BOTTLE_FILL_DRAGONBREATH, SoundCategory.PLAYERS, 1F, 1F);
}
player.swingArm(event.getHand());
}
}
} }

View file

@ -12,9 +12,10 @@ import net.minecraftforge.eventbus.api.SubscribeEvent;
public class BirthSpirit extends Glowing { public class BirthSpirit extends Glowing {
public BirthSpirit() { public BirthSpirit() {
super("birth_spirit"); super("birth_spirit");
MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.register(new EventHandler());
} }
private static class EventHandler {
@SubscribeEvent @SubscribeEvent
public void onBabyBorn(BabyEntitySpawnEvent event) { public void onBabyBorn(BabyEntitySpawnEvent event) {
LivingEntity parent = event.getParentA(); LivingEntity parent = event.getParentA();
@ -33,4 +34,5 @@ public class BirthSpirit extends Glowing {
IAuraChunk.getAuraChunk(parent.world, spot).drainAura(spot, 800 * amount); IAuraChunk.getAuraChunk(parent.world, spot).drainAura(spot, 800 * amount);
} }
} }
}
} }

View file

@ -29,7 +29,7 @@ public class RangeVisualizer extends ItemImpl {
public RangeVisualizer() { public RangeVisualizer() {
super("range_visualizer", new Properties().maxStackSize(1).group(NaturesAura.CREATIVE_TAB)); super("range_visualizer", new Properties().maxStackSize(1).group(NaturesAura.CREATIVE_TAB));
MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.register(new EventHandler());
} }
@Override @Override
@ -76,10 +76,12 @@ public class RangeVisualizer extends ItemImpl {
} }
} }
public class EventHandler {
@SubscribeEvent @SubscribeEvent
public void onInteract(PlayerInteractEvent.EntityInteractSpecific event) { public void onInteract(PlayerInteractEvent.EntityInteractSpecific event) {
ItemStack stack = event.getItemStack(); ItemStack stack = event.getItemStack();
if (stack.isEmpty() || stack.getItem() != this) if (stack.isEmpty() || stack.getItem() != RangeVisualizer.this)
return; return;
Entity entity = event.getTarget(); Entity entity = event.getTarget();
if (entity instanceof IVisualizable) { if (entity instanceof IVisualizable) {
@ -92,4 +94,5 @@ public class RangeVisualizer extends ItemImpl {
event.setCanceled(true); event.setCanceled(true);
} }
} }
}
} }

View file

@ -33,22 +33,10 @@ public class Armor extends ArmorItem implements IModItem, IModelProvider {
public Armor(String baseName, IArmorMaterial materialIn, EquipmentSlotType equipmentSlotIn) { public Armor(String baseName, IArmorMaterial materialIn, EquipmentSlotType equipmentSlotIn) {
super(materialIn, equipmentSlotIn, new Properties().group(NaturesAura.CREATIVE_TAB)); super(materialIn, equipmentSlotIn, new Properties().group(NaturesAura.CREATIVE_TAB));
this.baseName = baseName; this.baseName = baseName;
MinecraftForge.EVENT_BUS.register(this); MinecraftForge.EVENT_BUS.register(new EventHandler());
ModRegistry.add(this); ModRegistry.add(this);
} }
@SubscribeEvent
public void onAttack(LivingAttackEvent event) {
LivingEntity entity = event.getEntityLiving();
if (!entity.world.isRemote) {
if (!isFullSetEquipped(entity, 0))
return;
Entity source = event.getSource().getTrueSource();
if (source instanceof LivingEntity)
((LivingEntity) source).addPotionEffect(new EffectInstance(Effects.WITHER, 40));
}
}
@Override @Override
public String getBaseName() { public String getBaseName() {
return this.baseName; return this.baseName;
@ -75,4 +63,19 @@ public class Armor extends ArmorItem implements IModItem, IModelProvider {
} }
return true; return true;
} }
private static class EventHandler {
@SubscribeEvent
public void onAttack(LivingAttackEvent event) {
LivingEntity entity = event.getEntityLiving();
if (!entity.world.isRemote) {
if (!isFullSetEquipped(entity, 0))
return;
Entity source = event.getSource().getTrueSource();
if (source instanceof LivingEntity)
((LivingEntity) source).addPotionEffect(new EffectInstance(Effects.WITHER, 40));
}
}
}
} }

View file

@ -70,7 +70,6 @@ public class PacketAuraChunk {
return true; return true;
} }
@OnlyIn(Dist.CLIENT)
public static void onMessage(PacketAuraChunk message, Supplier<NetworkEvent.Context> ctx) { public static void onMessage(PacketAuraChunk message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> ClientEvents.PENDING_AURA_CHUNKS.add(message)); ctx.get().enqueueWork(() -> ClientEvents.PENDING_AURA_CHUNKS.add(message));
ctx.get().setPacketHandled(true); ctx.get().setPacketHandled(true);

View file

@ -5,8 +5,6 @@ import net.minecraft.client.Minecraft;
import net.minecraft.network.PacketBuffer; import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.dimension.DimensionType; import net.minecraft.world.dimension.DimensionType;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.network.NetworkEvent; import net.minecraftforge.fml.network.NetworkEvent;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -41,9 +39,12 @@ public class PacketClient {
buf.writeInt(i); buf.writeInt(i);
} }
@OnlyIn(Dist.CLIENT) // lambda causes classloading issues on a server here
@SuppressWarnings("Convert2Lambda")
public static void onMessage(PacketClient message, Supplier<NetworkEvent.Context> ctx) { public static void onMessage(PacketClient message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> { ctx.get().enqueueWork(new Runnable() {
@Override
public void run() {
Minecraft mc = Minecraft.getInstance(); Minecraft mc = Minecraft.getInstance();
if (mc.world != null) { if (mc.world != null) {
switch (message.type) { switch (message.type) {
@ -53,6 +54,7 @@ public class PacketClient {
RangeVisualizer.visualize(mc.player, RangeVisualizer.VISUALIZED_RAILS, DimensionType.getById(goalDim), goalPos); RangeVisualizer.visualize(mc.player, RangeVisualizer.VISUALIZED_RAILS, DimensionType.getById(goalDim), goalPos);
} }
} }
}
}); });
ctx.get().setPacketHandled(true); ctx.get().setPacketHandled(true);
} }

View file

@ -65,7 +65,6 @@ public class PacketParticleStream {
buf.writeFloat(packet.scale); buf.writeFloat(packet.scale);
} }
@OnlyIn(Dist.CLIENT)
public static void onMessage(PacketParticleStream message, Supplier<NetworkEvent.Context> ctx) { public static void onMessage(PacketParticleStream message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> NaturesAuraAPI.instance().spawnParticleStream( ctx.get().enqueueWork(() -> NaturesAuraAPI.instance().spawnParticleStream(
message.startX, message.startY, message.startZ, message.startX, message.startY, message.startZ,

View file

@ -12,8 +12,6 @@ import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeColors; import net.minecraft.world.biome.BiomeColors;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.network.NetworkEvent; import net.minecraftforge.fml.network.NetworkEvent;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -65,9 +63,12 @@ public class PacketParticles {
} }
} }
@OnlyIn(Dist.CLIENT) // lambda causes classloading issues on a server here
@SuppressWarnings("Convert2Lambda")
public static void onMessage(PacketParticles message, Supplier<NetworkEvent.Context> ctx) { public static void onMessage(PacketParticles message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> { ctx.get().enqueueWork(new Runnable() {
@Override
public void run() {
World world = Minecraft.getInstance().world; World world = Minecraft.getInstance().world;
if (world != null) { if (world != null) {
switch (message.type) { switch (message.type) {
@ -434,6 +435,7 @@ public class PacketParticles {
world.rand.nextGaussian() * 0.03F); world.rand.nextGaussian() * 0.03F);
} }
} }
}
}); });
ctx.get().setPacketHandled(true); ctx.get().setPacketHandled(true);
} }

View file

@ -36,6 +36,8 @@ public class PlayerLayerTrinkets extends LayerRenderer<AbstractClientPlayerEntit
this.alreadyRendered.clear(); this.alreadyRendered.clear();
GlStateManager.pushMatrix(); GlStateManager.pushMatrix();
GlStateManager.pushLightingAttributes();
GlStateManager.pushTextureAttributes();
GlStateManager.color4f(1F, 1F, 1F, 1F); GlStateManager.color4f(1F, 1F, 1F, 1F);
this.render(player, RenderType.BODY, main, second); this.render(player, RenderType.BODY, main, second);
float yaw = player.prevRotationYawHead + (player.rotationYawHead - player.prevRotationYawHead) * partialTicks; float yaw = player.prevRotationYawHead + (player.rotationYawHead - player.prevRotationYawHead) * partialTicks;
@ -45,6 +47,8 @@ public class PlayerLayerTrinkets extends LayerRenderer<AbstractClientPlayerEntit
GlStateManager.rotatef(yaw - 270, 0, 1, 0); GlStateManager.rotatef(yaw - 270, 0, 1, 0);
GlStateManager.rotatef(pitch, 0, 0, 1); GlStateManager.rotatef(pitch, 0, 0, 1);
this.render(player, RenderType.HEAD, main, second); this.render(player, RenderType.HEAD, main, second);
GlStateManager.popAttributes();
GlStateManager.popAttributes();
GlStateManager.popMatrix(); GlStateManager.popMatrix();
} }

View file

@ -45,7 +45,7 @@ public class SupporterFancyHandler {
Minecraft mc = Minecraft.getInstance(); Minecraft mc = Minecraft.getInstance();
if (player == mc.player && mc.gameSettings.thirdPersonView == 0) if (player == mc.player && mc.gameSettings.thirdPersonView == 0)
return; return;
FancyInfo info = FANCY_INFOS.get(player.getName()); FancyInfo info = FANCY_INFOS.get(player.getGameProfile().getName());
if (info == null) if (info == null)
return; return;

View file

@ -7,8 +7,7 @@
"faces": { "faces": {
"down": { "down": {
"uv": [4, 4, 12, 12], "uv": [4, 4, 12, 12],
"texture": "#texture", "texture": "#texture"
"cullface": "down"
}, },
"up": { "up": {
"uv": [4, 4, 12, 12], "uv": [4, 4, 12, 12],
@ -16,23 +15,19 @@
}, },
"north": { "north": {
"uv": [4, 4, 12, 12], "uv": [4, 4, 12, 12],
"texture": "#texture", "texture": "#texture"
"cullface": "north"
}, },
"south": { "south": {
"uv": [4, 4, 12, 12], "uv": [4, 4, 12, 12],
"texture": "#texture", "texture": "#texture"
"cullface": "south"
}, },
"west": { "west": {
"uv": [4, 4, 12, 12], "uv": [4, 4, 12, 12],
"texture": "#texture", "texture": "#texture"
"cullface": "west"
}, },
"east": { "east": {
"uv": [4, 4, 12, 12], "uv": [4, 4, 12, 12],
"texture": "#texture", "texture": "#texture"
"cullface": "east"
} }
} }
} }