-
Notifications
You must be signed in to change notification settings - Fork 42
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
Feature Request / Fix: Energy Control Mod Compat #551
base: MC1.12
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
package cr0s.warpdrive.compat; | ||
|
||
import cr0s.warpdrive.WarpDrive; | ||
import cr0s.warpdrive.api.IBlockTransformer; | ||
import cr0s.warpdrive.api.ITransformation; | ||
import cr0s.warpdrive.api.WarpDriveText; | ||
import cr0s.warpdrive.config.WarpDriveConfig; | ||
import net.minecraft.block.Block; | ||
import net.minecraft.block.state.IBlockState; | ||
import net.minecraft.nbt.NBTBase; | ||
import net.minecraft.nbt.NBTTagCompound; | ||
import net.minecraft.nbt.NBTTagList; | ||
import net.minecraft.tileentity.TileEntity; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.World; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
|
||
public class CompatEnergyControl implements IBlockTransformer { | ||
|
||
//only require rotation | ||
private static Class<?> classThermalMonitor; //Thermal Monitor | ||
private static Class<?> classRemoteThermalMonitor; //Remote Thermal Monitor | ||
|
||
//rotation and coordinate | ||
private static Class<?> classInfoPanel; //(Advanced) Info Panel | ||
private static Class<?> classInfoPanelExtender; //(Advanced) Info Panel Extender | ||
private static Class<?> classHoloPanel; //Holographic Panel | ||
private static Class<?> classHoloPanelExtender; //Holo Extender | ||
|
||
private static Class<?> classRangeTrigger; //Range Trigger | ||
|
||
public static void register(){ | ||
try{ | ||
classThermalMonitor = Class.forName("com.zuxelus.energycontrol.blocks.ThermalMonitor"); | ||
classRemoteThermalMonitor = Class.forName("com.zuxelus.energycontrol.blocks.RemoteThermalMonitor"); | ||
classInfoPanel = Class.forName("com.zuxelus.energycontrol.blocks.InfoPanel"); | ||
classInfoPanelExtender = Class.forName("com.zuxelus.energycontrol.blocks.InfoPanelExtender"); | ||
classHoloPanel = Class.forName("com.zuxelus.energycontrol.blocks.HoloPanel"); | ||
classHoloPanelExtender = Class.forName("com.zuxelus.energycontrol.blocks.HoloPanelExtender"); | ||
classRangeTrigger = Class.forName("com.zuxelus.energycontrol.blocks.RangeTrigger"); | ||
WarpDriveConfig.registerBlockTransformer("energycontrol", new CompatEnergyControl()); | ||
}catch(final ClassNotFoundException exception){ | ||
WarpDrive.logger.error(exception); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider printing the stack trace like other compatibility module do? |
||
} | ||
} | ||
|
||
@Override | ||
public boolean isApplicable(Block block, int metadata, TileEntity tileEntity) { | ||
return ( | ||
classThermalMonitor.isInstance(block) || | ||
classRemoteThermalMonitor.isInstance(block) || | ||
classInfoPanel.isInstance(block) || | ||
classInfoPanelExtender.isInstance(block) || | ||
classHoloPanel.isInstance(block) || | ||
classHoloPanelExtender.isInstance(block) | ||
); | ||
} | ||
|
||
@Override | ||
public boolean isJumpReady(Block block, int metadata, TileEntity tileEntity, WarpDriveText reason) { | ||
//nothing to do | ||
return true; | ||
} | ||
|
||
@Override | ||
public NBTBase saveExternals(World world, int x, int y, int z, Block block, int blockMeta, TileEntity tileEntity) { | ||
//nothing to do | ||
return null; | ||
} | ||
|
||
@Override | ||
public void removeExternals(World world, int x, int y, int z, Block block, int blockMeta, TileEntity tileEntity) { | ||
//nothing to do | ||
} | ||
|
||
//Rotation ID/metadata 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ||
private static final byte[] panelRotFacing = { 0, 1, 5, 4, 2, 3, 6, 7, 11, 10, 8, 9, 12, 13, 14, 15}; | ||
private static final byte[] nbtRotFacing = { 0, 1, 5, 4, 2, 3, 0, 1, 2, 3, 4, 5, 12, 13, 14, 15}; | ||
private static final byte[] horizontalRotFacing = { 1, 2, 3, 0, 5, 6, 7, 4, 8, 9, 10, 11, 12, 13, 14, 15}; | ||
|
||
@Override | ||
public int rotate(Block block, int metadata, NBTTagCompound nbtTileEntity, ITransformation transformation) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameters should all be final by default, consider updating accordingly? |
||
final byte rotationSteps = transformation.getRotationSteps(); | ||
|
||
|
||
//Fix screen data for display panels | ||
if( | ||
classInfoPanel.isInstance(block) || | ||
classHoloPanel.isInstance(block) | ||
){ | ||
NBTTagCompound screenData = nbtTileEntity.getCompoundTag("screenData"); | ||
int maxX = screenData.getInteger("maxX"); | ||
int maxY = screenData.getInteger("maxY"); | ||
int maxZ = screenData.getInteger("maxZ"); | ||
int minX = screenData.getInteger("minX"); | ||
int minY = screenData.getInteger("minY"); | ||
int minZ = screenData.getInteger("minZ"); | ||
BlockPos newMax = transformation.apply(maxX, maxY, maxZ); | ||
BlockPos newMin = transformation.apply(minX, minY, minZ); | ||
int newMaxX = Math.max(newMax.getX(), newMin.getX()); | ||
int newMaxY = Math.max(newMax.getY(), newMin.getY()); | ||
int newMaxZ = Math.max(newMax.getZ(), newMin.getZ()); | ||
int newMinX = Math.min(newMax.getX(), newMin.getX()); | ||
int newMinY = Math.min(newMax.getY(), newMin.getY()); | ||
int newMinZ = Math.min(newMax.getZ(), newMin.getZ()); | ||
|
||
screenData.setInteger("maxX", newMaxX); | ||
screenData.setInteger("maxY", newMaxY); | ||
screenData.setInteger("maxZ", newMaxZ); | ||
screenData.setInteger("minX", newMinX); | ||
screenData.setInteger("minY", newMinY); | ||
screenData.setInteger("minZ", newMinZ); | ||
nbtTileEntity.setTag("screenData", screenData); | ||
} | ||
|
||
|
||
//handle data cards (transform absolute coordinates) | ||
if( | ||
classHoloPanel.isInstance(block) || | ||
classInfoPanel.isInstance(block) || | ||
classRemoteThermalMonitor.isInstance(block) || | ||
classRangeTrigger.isInstance(block) | ||
){ | ||
NBTTagList items = nbtTileEntity.getTagList("Items", 10); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid harcoded values, consider using the NBTType.xxx related constant instead? |
||
NBTTagList itemsNew = items.copy(); | ||
boolean anyChange = false; | ||
for(int index = 0; index < items.tagCount(); index++){ | ||
NBTTagCompound item = items.getCompoundTagAt(index); | ||
if(!item.hasKey("tag")){ continue; } //must have tag | ||
NBTTagCompound itemTag = item.getCompoundTag("tag"); | ||
if(!(itemTag.hasKey("x") && | ||
itemTag.hasKey("y") && | ||
itemTag.hasKey("z"))){ | ||
continue; | ||
} //must have the coordinate data | ||
|
||
int x = itemTag.getInteger("x"); | ||
int y = itemTag.getInteger("y"); | ||
int z = itemTag.getInteger("z"); | ||
|
||
if(!transformation.isInside(x, y, z)){ continue; }//only convert if inside the ship | ||
BlockPos result = transformation.apply(x, y, z); | ||
itemTag.setInteger("x", result.getX()); | ||
itemTag.setInteger("y", result.getY()); | ||
itemTag.setInteger("z", result.getZ()); | ||
item.setTag("tag", itemTag); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This modify the original item, then overwrite it with a copy, consider skipping the copy and just modify in place? and by extension, no need to call |
||
itemsNew.set(index, item); | ||
anyChange = true; | ||
} | ||
if(anyChange){ | ||
nbtTileEntity.setTag("Items", itemsNew); | ||
} | ||
} | ||
|
||
|
||
//Redirect core x/y/z for extended screens | ||
if( | ||
classInfoPanelExtender.isInstance(block)|| | ||
classHoloPanelExtender.isInstance(block) | ||
){ | ||
byte partOfScreen = nbtTileEntity.getByte("partOfScreen"); | ||
//only do the modification if it is part of a screen | ||
if(partOfScreen == 1){ | ||
int x = nbtTileEntity.getInteger("coreX"); | ||
int y = nbtTileEntity.getInteger("coreY"); | ||
int z = nbtTileEntity.getInteger("coreZ"); | ||
BlockPos result = transformation.apply(x, y, z); | ||
nbtTileEntity.setInteger("coreX", result.getX()); | ||
nbtTileEntity.setInteger("coreY", result.getY()); | ||
nbtTileEntity.setInteger("coreZ", result.getZ()); | ||
} | ||
} | ||
|
||
|
||
//handle rotation in NBT | ||
//6 sided type (block screens and monitors) - both facing and rotation | ||
if( | ||
classThermalMonitor.isInstance(block) || | ||
classRemoteThermalMonitor.isInstance(block) || | ||
classInfoPanel.isInstance(block) || | ||
classInfoPanelExtender.isInstance(block) | ||
){ | ||
int facing = nbtTileEntity.getInteger("facing"); | ||
int rotation = 15; | ||
//only panels have rotation | ||
//NOTE there is no rotation of 15, so we use it as a marker of "Not exist" | ||
if(nbtTileEntity.hasKey("rotation")){ | ||
rotation = nbtTileEntity.getInteger("rotation"); | ||
} | ||
switch (rotationSteps) { | ||
//without break, so it rotate an additional time for each step. | ||
case 3: | ||
metadata = panelRotFacing[metadata]; | ||
facing = nbtRotFacing[facing]; | ||
rotation = nbtRotFacing[rotation]; | ||
case 2: | ||
metadata = panelRotFacing[metadata]; | ||
facing = nbtRotFacing[facing]; | ||
rotation = nbtRotFacing[rotation]; | ||
case 1: | ||
metadata = panelRotFacing[metadata]; | ||
facing = nbtRotFacing[facing]; | ||
rotation = nbtRotFacing[rotation]; | ||
default: | ||
break; | ||
} | ||
nbtTileEntity.setInteger("facing", facing); | ||
if(rotation != 15){ | ||
nbtTileEntity.setInteger("rotation", rotation); | ||
} | ||
} | ||
|
||
|
||
//4 sided type (holo displays and range trigger) - facing only | ||
if( | ||
classHoloPanel.isInstance(block) || | ||
classHoloPanelExtender.isInstance(block) || | ||
classRangeTrigger.isInstance(block) | ||
){ | ||
int facing = nbtTileEntity.getInteger("facing"); | ||
switch (rotationSteps) { | ||
//without break, so it rotate an additional time for each step. | ||
case 3: | ||
metadata = horizontalRotFacing[metadata]; | ||
facing = nbtRotFacing[facing]; | ||
case 2: | ||
metadata = horizontalRotFacing[metadata]; | ||
facing = nbtRotFacing[facing]; | ||
case 1: | ||
metadata = horizontalRotFacing[metadata]; | ||
facing = nbtRotFacing[facing]; | ||
default: | ||
break; | ||
} | ||
nbtTileEntity.setInteger("facing", facing); | ||
} | ||
|
||
return metadata; | ||
} | ||
|
||
@Override | ||
public void restoreExternals(World world, BlockPos blockPos, IBlockState blockState, TileEntity tileEntity, ITransformation transformation, NBTBase nbtBase) { | ||
//nothing to do | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -354,6 +354,10 @@ public BlockPos deploy(final World worldSource, final World worldTarget, final I | |
nbtToDeploy.setInteger("y", target.getY()); | ||
nbtToDeploy.setInteger("z", target.getZ()); | ||
|
||
/* | ||
//NOTE: this was the original NuclearControl compatibility. | ||
//NOTE: However this is incorrect, as it may result in flipping the screen. | ||
//NOTE: See CompatEnergyControl.java in mod compat for updated implementation. | ||
if (nbtToDeploy.hasKey("screenData")) {// IC2NuclearControl 2.2.5a | ||
final NBTTagCompound nbtScreenData = nbtToDeploy.getCompoundTag("screenData"); | ||
if ( nbtScreenData.hasKey("minX") && nbtScreenData.hasKey("minY") && nbtScreenData.hasKey("minZ") | ||
|
@@ -373,7 +377,7 @@ public BlockPos deploy(final World worldSource, final World worldTarget, final I | |
nbtToDeploy.setTag("screenData", nbtScreenData); | ||
} | ||
} | ||
|
||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider removing the legacy NuclearControl code altogether? |
||
TileEntity newTileEntity = null; | ||
boolean isForgeMultipart = false; | ||
if ( WarpDriveConfig.isForgeMultipartLoaded | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider following existing code spacing rules?