* Upgrades are now dropped when the screen is destroyed

This commit is contained in:
Nicolas BARBOTIN 2018-01-30 20:59:36 +01:00
parent 1fd101ddbe
commit 8dc5a0e5d6
3 changed files with 46 additions and 27 deletions

View File

@ -11,7 +11,6 @@ This is the unfinished port of the WebDisplays mod for Minecraft 1.12.2. The tex
### TODO
* VideoType doesn't seem to be used...
* DROP UPGRADES WHEN SCREEN IS DESTROYED
* Achievements (minePad 2 and all that stuff)
* Top/bottom screen orientation
* GuiSetURL2 missing buttons

View File

@ -227,34 +227,38 @@ public class BlockScreen extends WDBlockContainer {
return meta == 0 ? null : new TileEntityScreen();
}
@Override
public void onBlockDestroyedByPlayer(World world, BlockPos pos, IBlockState dontCare) {
/************************************************* DESTRUCTION HANDLING *************************************************/
private void onDestroy(World world, BlockPos pos, EntityPlayer ply) {
if(!world.isRemote) {
Vector3i bp = new Vector3i(pos);
Multiblock.BlockOverride override = new Multiblock.BlockOverride(bp, Multiblock.OverrideAction.SIMULATE);
for(BlockSide bs: BlockSide.values())
destroySide(world, bp.clone(), bs, override);
destroySide(world, bp.clone(), bs, override, ply);
}
}
private void destroySide(World world, Vector3i pos, BlockSide side, Multiblock.BlockOverride override) {
private void destroySide(World world, Vector3i pos, BlockSide side, Multiblock.BlockOverride override, EntityPlayer source) {
Multiblock.findOrigin(world, pos, side, override);
BlockPos bp = pos.toBlock();
TileEntity te = world.getTileEntity(bp);
if(te != null && te instanceof TileEntityScreen)
if(te != null && te instanceof TileEntityScreen) {
((TileEntityScreen) te).onDestroy(source);
world.setBlockState(bp, getDefaultState().withProperty(hasTE, false)); //Destroy tile entity
}
}
@Override
public EnumPushReaction getMobilityFlag(IBlockState state) {
return EnumPushReaction.IGNORE;
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer ply, boolean willHarvest) {
onDestroy(world, pos, ply);
return super.removedByPlayer(state, world, pos, ply, willHarvest);
}
@Override
public void onBlockDestroyedByExplosion(World world, BlockPos pos, Explosion explosion) {
onBlockDestroyedByPlayer(world, pos, null);
onDestroy(world, pos, null);
}
@Override
@ -275,9 +279,14 @@ public class BlockScreen extends WDBlockContainer {
for(Vector3i neighbor: neighbors) {
if(world.getBlockState(neighbor.toBlock()).getBlock() instanceof BlockScreen) {
for(BlockSide bs: BlockSide.values())
destroySide(world, neighbor.clone(), bs, override);
destroySide(world, neighbor.clone(), bs, override, (whoDidThisShit instanceof EntityPlayer) ? ((EntityPlayer) whoDidThisShit) : null);
}
}
}
@Override
public EnumPushReaction getMobilityFlag(IBlockState state) {
return EnumPushReaction.IGNORE;
}
}

View File

@ -719,23 +719,7 @@ public class TileEntityScreen extends TileEntity {
}
if(idxToRemove >= 0) {
if(!itemAsUpgrade.onRemove(this, side, player, scr.upgrades.get(idxToRemove))) { //Drop upgrade item
ItemStack toDrop = scr.upgrades.get(idxToRemove);
boolean spawnDrop = true;
if(player != null) {
if(player.isCreative() || player.addItemStackToInventory(toDrop))
spawnDrop = false; //If in creative or if the item was added to the player's inventory, don't spawn drop entity
}
if(spawnDrop) {
Vector3f pos = new Vector3f((float) this.pos.getX(), (float) this.pos.getY(), (float) this.pos.getZ());
pos.addMul(side.backward.toFloat(), 1.5f);
world.spawnEntity(new EntityItem(world, (double) pos.x, (double) pos.y, (double) pos.z, toDrop));
}
}
dropUpgrade(scr.upgrades.get(idxToRemove), side, player);
scr.upgrades.remove(idxToRemove);
WebDisplays.NET_HANDLER.sendToAllAround(CMessageScreenUpdate.upgrade(this, side), point());
playSoundAt(WebDisplays.INSTANCE.soundUpgradeDel, pos, 1.0f, 1.0f);
@ -743,6 +727,24 @@ public class TileEntityScreen extends TileEntity {
Log.warning("Tried to remove non-existing upgrade %s to screen %s at %s", safeName(is), side.toString(), pos.toString());
}
private void dropUpgrade(ItemStack is, BlockSide side, @Nullable EntityPlayer ply) {
if(!((IUpgrade) is.getItem()).onRemove(this, side, ply, is)) { //Drop upgrade item
boolean spawnDrop = true;
if(ply != null) {
if(ply.isCreative() || ply.addItemStackToInventory(is))
spawnDrop = false; //If in creative or if the item was added to the player's inventory, don't spawn drop entity
}
if(spawnDrop) {
Vector3f pos = new Vector3f((float) this.pos.getX(), (float) this.pos.getY(), (float) this.pos.getZ());
pos.addMul(side.backward.toFloat(), 1.5f);
world.spawnEntity(new EntityItem(world, (double) pos.x, (double) pos.y, (double) pos.z, is));
}
}
}
private Screen getScreenForLaserOp(BlockSide side, EntityPlayer ply) {
if(world.isRemote)
return null;
@ -795,4 +797,13 @@ public class TileEntityScreen extends TileEntity {
}
}
public void onDestroy(@Nullable EntityPlayer ply) {
for(Screen scr: screens) {
for(ItemStack is: scr.upgrades)
dropUpgrade(is, scr.side, ply);
scr.upgrades.clear();
}
}
}