Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to automatically wake up personal & passive anchors on owner login #12

Merged
merged 3 commits into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies {
compileOnly files("libs/HungerOverhaul-1.7.10-1.0.2.jar")
compileOnly files("libs/Thaumcraft-1.7.10-4.2.3.5-dev.jar")
compileOnly files("libs/CoFHCore-[1.7.10]3.1.4-329-dev.jar")
compileOnly files("libs/railcraft-1.7.10-9.12.2.0.jar")
}


Expand Down
Binary file added libs/Railcraft_1.7.10-9.12.2.0.jar
Binary file not shown.
6 changes: 5 additions & 1 deletion src/main/java/com/mitchej123/hodgepodge/Hodgepodge.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mitchej123.hodgepodge;

import com.mitchej123.hodgepodge.core.HodgePodgeClient;
import com.mitchej123.hodgepodge.core.util.AnchorAlarm;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
Expand All @@ -18,9 +20,11 @@ public class Hodgepodge {
public static final String VERSION = "GRADLETOKEN_VERSION";
public static final String NAME = "A Hodgepodge of Patches";
public static XSTR RNG = new XSTR();
public final static AnchorAlarm ANCHOR_ALARM = new AnchorAlarm();

@EventHandler
public void init(FMLInitializationEvent asd) {
public void init(FMLInitializationEvent event) {
FMLCommonHandler.instance().bus().register(ANCHOR_ALARM);
}

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ public enum MixinSets {
DROP_PICKED_LOOT_ON_DESPAWN("Drop picked loot on entity despawn",
() -> config.dropPickedLootOnDespawn,
Collections.singletonList("dropPickedLootOnDespawn.MixinEntityLiving")
),
WAKE_ANCHORS_ON_LOGIN("Wake up personal anchors on owner login",
() -> config.installAnchorAlarm,
"Railcraft_1.7.10",
Arrays.asList(
"wakePersonalAnchors.MixinTileAnchorPassive",
"wakePersonalAnchors.MixinTileAnchorPersonal")
);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class LoadingConfig {
public boolean removeUpdateChecks;
public boolean preventPickupLoot;
public boolean dropPickedLootOnDespawn;

public boolean installAnchorAlarm;
// ASM
public boolean pollutionAsm;
public boolean cofhWorldTransformer;
Expand All @@ -44,6 +44,7 @@ public LoadingConfig(File file) {
fixThaumcraftUnprotectedGetBlock = config.get("fixes", "fixThaumcraftUnprotectedGetBlock", true, "Various Thaumcraft unchecked getBlock() patches").getBoolean();
fixHungerOverhaul = config.get("fixes", "fixHungerOverhaul", true, "Fix hunger overhaul low stat effects").getBoolean();
removeUpdateChecks = config.get("fixes", "removeUpdateChecks", true, "Remove old/stale/outdated update checks.").getBoolean();
installAnchorAlarm = config.get("tweaks", "installAnchorAlarm", true, "Wake up passive & personal anchors on player login").getBoolean();

preventPickupLoot = config.get("tweaks", "preventPickupLoot", true, "Prevent monsters from picking up loot.").getBoolean();
dropPickedLootOnDespawn = config.get("tweaks", "dropPickedLootOnDespawn", true, "Drop picked loot on entity despawn").getBoolean();
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/com/mitchej123/hodgepodge/core/util/AnchorAlarm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.mitchej123.hodgepodge.core.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.WorldServer;

import net.minecraftforge.common.DimensionManager;

import com.mitchej123.hodgepodge.Hodgepodge;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import mods.railcraft.common.blocks.machine.alpha.TileAnchorWorld;
import mods.railcraft.common.plugins.forge.PlayerPlugin;

public class AnchorAlarm {
private static final String NBT_KEY = "GT_RC_AnchorAlarmList";
public static void addNewAnchor(EntityLivingBase entityliving, TileEntity te) {
if (entityliving instanceof EntityPlayerMP) {
byte[] oldbuf = null;
if (entityliving.getEntityData().hasKey(NBT_KEY))
oldbuf = entityliving.getEntityData().getByteArray(NBT_KEY);
byte[] newbuf = new byte[(oldbuf == null ? 0 : oldbuf.length) + 16];
ByteBuf newbufwrap = Unpooled.wrappedBuffer(newbuf);
newbufwrap.setIndex(0, 0);
if (oldbuf != null)
newbufwrap.writeBytes(oldbuf);
saveCoordinatesToPlayer(entityliving, newbufwrap, newbuf, te);
}
}
private static void saveCoordinatesToPlayer(EntityLivingBase player, ByteBuf buf, byte[] bytes, TileEntity te) {
buf.writeInt(te.getWorldObj().provider.dimensionId);
buf.writeInt(te.xCoord);
buf.writeInt(te.yCoord);
buf.writeInt(te.zCoord);
player.getEntityData().setByteArray(NBT_KEY, bytes);
}
@SuppressWarnings("unused")
@SubscribeEvent
public void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (event.player instanceof EntityPlayerMP) {
if (event.player.getEntityData().hasKey(NBT_KEY)) {
byte[] bytes = event.player.getEntityData().getByteArray(NBT_KEY);
ByteBuf buf = Unpooled.wrappedBuffer(bytes);
int N = bytes.length / 16;
ByteBuf newbuf = Unpooled.buffer(bytes.length);
int validAlarmCount = 0;
try {
Set<TileEntity> loadedTiles = new HashSet<>();
for (int i = 0; i < N; ++i) {
int dim = buf.readInt();
int x = buf.readInt();
int y = buf.readInt();
int z = buf.readInt();
WorldServer w = DimensionManager.getWorld(dim);
if (w != null) {
// if there is some different tile at the place, ok, we will load this chunk one last time
w.getChunkProvider().loadChunk(x >> 4, z >> 4);
TileEntity t = w.getTileEntity(x, y, z);
if (loadedTiles.contains(t))
continue;
loadedTiles.add(t);
if (t instanceof TileAnchorWorld) {
if (PlayerPlugin.isSamePlayer(((TileAnchorWorld)t).getOwner(), event.player.getGameProfile())) {
// if there is still our tile there, save it for later
++validAlarmCount;
newbuf.writeInt(dim);
newbuf.writeInt(x);
newbuf.writeInt(y);
newbuf.writeInt(z);
}
}
}
}
}
catch (IndexOutOfBoundsException ignored) {
Hodgepodge.log.error("Error reading anchor list!");
}
byte[] newbytes = new byte[validAlarmCount * 16];
newbuf.readBytes(newbytes);
event.player.getEntityData().setByteArray(NBT_KEY, newbytes);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mitchej123.hodgepodge.mixins.wakePersonalAnchors;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;

import com.mitchej123.hodgepodge.core.util.AnchorAlarm;
import mods.railcraft.common.blocks.machine.alpha.TileAnchorPassive;
import mods.railcraft.common.blocks.machine.alpha.TileAnchorWorld;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(TileAnchorPassive.class)
public class MixinTileAnchorPassive extends TileAnchorWorld {
@Override
public void onBlockPlacedBy(EntityLivingBase entityliving, ItemStack stack) {
super.onBlockPlacedBy(entityliving, stack);
AnchorAlarm.addNewAnchor(entityliving, this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mitchej123.hodgepodge.mixins.wakePersonalAnchors;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;

import com.mitchej123.hodgepodge.core.util.AnchorAlarm;
import mods.railcraft.common.blocks.machine.alpha.TileAnchorPersonal;
import mods.railcraft.common.blocks.machine.alpha.TileAnchorWorld;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(TileAnchorPersonal.class)
public class MixinTileAnchorPersonal extends TileAnchorWorld {
@Override
public void onBlockPlacedBy(EntityLivingBase entityliving, ItemStack stack) {
super.onBlockPlacedBy(entityliving, stack);
AnchorAlarm.addNewAnchor(entityliving, this);
}
}