Add piston events (#5798)
This commit is contained in:
parent
a8a0306f98
commit
50e411b82a
4 changed files with 321 additions and 4 deletions
|
@ -1,6 +1,22 @@
|
||||||
--- a/net/minecraft/block/PistonBlock.java
|
--- a/net/minecraft/block/PistonBlock.java
|
||||||
+++ b/net/minecraft/block/PistonBlock.java
|
+++ b/net/minecraft/block/PistonBlock.java
|
||||||
@@ -197,7 +197,7 @@
|
@@ -166,6 +166,7 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p_189539_4_ == 0) {
|
||||||
|
+ if(net.minecraftforge.event.ForgeEventFactory.onPistonMovePre(p_189539_2_, p_189539_3_, direction, true)) return false;
|
||||||
|
if (!this.func_176319_a(p_189539_2_, p_189539_3_, direction, true)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
@@ -173,6 +174,7 @@
|
||||||
|
p_189539_2_.func_180501_a(p_189539_3_, p_189539_1_.func_206870_a(field_176320_b, Boolean.valueOf(true)), 67);
|
||||||
|
p_189539_2_.func_184133_a((PlayerEntity)null, p_189539_3_, SoundEvents.field_187715_dR, SoundCategory.BLOCKS, 0.5F, p_189539_2_.field_73012_v.nextFloat() * 0.25F + 0.6F);
|
||||||
|
} else if (p_189539_4_ == 1 || p_189539_4_ == 2) {
|
||||||
|
+ if(net.minecraftforge.event.ForgeEventFactory.onPistonMovePre(p_189539_2_, p_189539_3_, direction, false)) return false;
|
||||||
|
TileEntity tileentity1 = p_189539_2_.func_175625_s(p_189539_3_.func_177972_a(direction));
|
||||||
|
if (tileentity1 instanceof PistonTileEntity) {
|
||||||
|
((PistonTileEntity)tileentity1).func_145866_f();
|
||||||
|
@@ -197,7 +199,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!flag1) {
|
if (!flag1) {
|
||||||
|
@ -9,7 +25,15 @@
|
||||||
p_189539_2_.func_217377_a(p_189539_3_.func_177972_a(direction), false);
|
p_189539_2_.func_217377_a(p_189539_3_.func_177972_a(direction), false);
|
||||||
} else {
|
} else {
|
||||||
this.func_176319_a(p_189539_2_, p_189539_3_, direction, false);
|
this.func_176319_a(p_189539_2_, p_189539_3_, direction, false);
|
||||||
@@ -238,7 +238,7 @@
|
@@ -210,6 +212,7 @@
|
||||||
|
p_189539_2_.func_184133_a((PlayerEntity)null, p_189539_3_, SoundEvents.field_187712_dQ, SoundCategory.BLOCKS, 0.5F, p_189539_2_.field_73012_v.nextFloat() * 0.15F + 0.6F);
|
||||||
|
}
|
||||||
|
|
||||||
|
+ net.minecraftforge.event.ForgeEventFactory.onPistonMovePost(p_189539_2_, p_189539_3_, direction, (p_189539_4_ == 0));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -238,7 +241,7 @@
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +42,7 @@
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -274,7 +274,7 @@
|
@@ -274,7 +277,7 @@
|
||||||
for(int j = list2.size() - 1; j >= 0; --j) {
|
for(int j = list2.size() - 1; j >= 0; --j) {
|
||||||
BlockPos blockpos2 = list2.get(j);
|
BlockPos blockpos2 = list2.get(j);
|
||||||
BlockState blockstate = p_176319_1_.func_180495_p(blockpos2);
|
BlockState blockstate = p_176319_1_.func_180495_p(blockpos2);
|
||||||
|
@ -27,7 +51,7 @@
|
||||||
func_220059_a(blockstate, p_176319_1_, blockpos2, tileentity);
|
func_220059_a(blockstate, p_176319_1_, blockpos2, tileentity);
|
||||||
p_176319_1_.func_180501_a(blockpos2, Blocks.field_150350_a.func_176223_P(), 18);
|
p_176319_1_.func_180501_a(blockpos2, Blocks.field_150350_a.func_176223_P(), 18);
|
||||||
--k;
|
--k;
|
||||||
@@ -328,6 +328,10 @@
|
@@ -328,6 +331,10 @@
|
||||||
return p_185499_1_.func_206870_a(field_176387_N, p_185499_2_.func_185831_a(p_185499_1_.func_177229_b(field_176387_N)));
|
return p_185499_1_.func_206870_a(field_176387_N, p_185499_2_.func_185831_a(p_185499_1_.func_177229_b(field_176387_N)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -126,6 +126,7 @@ import net.minecraftforge.event.world.BlockEvent.NeighborNotifyEvent;
|
||||||
import net.minecraftforge.event.world.ChunkWatchEvent;
|
import net.minecraftforge.event.world.ChunkWatchEvent;
|
||||||
import net.minecraftforge.event.world.ExplosionEvent;
|
import net.minecraftforge.event.world.ExplosionEvent;
|
||||||
import net.minecraftforge.event.world.GetCollisionBoxesEvent;
|
import net.minecraftforge.event.world.GetCollisionBoxesEvent;
|
||||||
|
import net.minecraftforge.event.world.PistonEvent;
|
||||||
import net.minecraftforge.event.world.SaplingGrowTreeEvent;
|
import net.minecraftforge.event.world.SaplingGrowTreeEvent;
|
||||||
import net.minecraftforge.event.world.WorldEvent;
|
import net.minecraftforge.event.world.WorldEvent;
|
||||||
import net.minecraftforge.eventbus.api.Event;
|
import net.minecraftforge.eventbus.api.Event;
|
||||||
|
@ -688,4 +689,14 @@ public class ForgeEventFactory
|
||||||
else
|
else
|
||||||
MinecraftForge.EVENT_BUS.post(new ChunkWatchEvent.UnWatch(entity, chunkpos, world));
|
MinecraftForge.EVENT_BUS.post(new ChunkWatchEvent.UnWatch(entity, chunkpos, world));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean onPistonMovePre(World world, BlockPos pos, Direction direction, boolean extending)
|
||||||
|
{
|
||||||
|
return MinecraftForge.EVENT_BUS.post(new PistonEvent.Pre(world, pos, direction, extending ? PistonEvent.PistonMoveType.EXTEND : PistonEvent.PistonMoveType.RETRACT));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean onPistonMovePost(World world, BlockPos pos, Direction direction, boolean extending)
|
||||||
|
{
|
||||||
|
return MinecraftForge.EVENT_BUS.post(new PistonEvent.Post(world, pos, direction, extending ? PistonEvent.PistonMoveType.EXTEND : PistonEvent.PistonMoveType.RETRACT));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
128
src/main/java/net/minecraftforge/event/world/PistonEvent.java
Normal file
128
src/main/java/net/minecraftforge/event/world/PistonEvent.java
Normal file
|
@ -0,0 +1,128 @@
|
||||||
|
/*
|
||||||
|
* Minecraft Forge
|
||||||
|
* Copyright (c) 2016-2019.
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation version 2.1
|
||||||
|
* of the License.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.minecraftforge.event.world;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import net.minecraft.block.state.PistonBlockStructureHelper;
|
||||||
|
import net.minecraft.util.Direction;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
import net.minecraftforge.eventbus.api.Cancelable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base piston event, use {@link PistonEvent.Post} and {@link PistonEvent.Pre}
|
||||||
|
*/
|
||||||
|
public abstract class PistonEvent extends BlockEvent
|
||||||
|
{
|
||||||
|
|
||||||
|
private final Direction direction;
|
||||||
|
private final PistonMoveType moveType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param world
|
||||||
|
* @param pos - The position of the piston
|
||||||
|
* @param direction - The direction of the piston
|
||||||
|
* @param direction - The move direction of the piston
|
||||||
|
*/
|
||||||
|
public PistonEvent(World world, BlockPos pos, Direction direction, PistonMoveType moveType)
|
||||||
|
{
|
||||||
|
super(world, pos, world.getBlockState(pos));
|
||||||
|
this.direction = direction;
|
||||||
|
this.moveType = moveType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The direction of the piston block
|
||||||
|
*/
|
||||||
|
public Direction getDirection()
|
||||||
|
{
|
||||||
|
return this.direction;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method that gets the piston position offset by its facing
|
||||||
|
*/
|
||||||
|
public BlockPos getFaceOffsetPos()
|
||||||
|
{
|
||||||
|
return this.getPos().offset(direction);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The movement type of the piston (extension, retraction)
|
||||||
|
*/
|
||||||
|
public PistonMoveType getPistonMoveType()
|
||||||
|
{
|
||||||
|
return moveType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return A piston structure helper for this movement. Returns null if the world stored is not a {@link World}
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public PistonBlockStructureHelper getStructureHelper()
|
||||||
|
{
|
||||||
|
if(this.getWorld() instanceof World) {
|
||||||
|
return new PistonBlockStructureHelper((World) this.getWorld(), this.getPos(), this.getDirection(), this.getPistonMoveType().isExtend);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fires after the piston has moved and set surrounding states. This will not fire if {@link PistonEvent.Pre} is cancelled.
|
||||||
|
*/
|
||||||
|
public static class Post extends PistonEvent
|
||||||
|
{
|
||||||
|
|
||||||
|
public Post(World world, BlockPos pos, Direction direction, PistonMoveType moveType)
|
||||||
|
{
|
||||||
|
super(world, pos, direction, moveType);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fires before the piston has updated block states. Cancellation prevents movement.
|
||||||
|
*/
|
||||||
|
@Cancelable
|
||||||
|
public static class Pre extends PistonEvent
|
||||||
|
{
|
||||||
|
|
||||||
|
public Pre(World world, BlockPos pos, Direction direction, PistonMoveType moveType)
|
||||||
|
{
|
||||||
|
super(world, pos, direction, moveType);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static enum PistonMoveType
|
||||||
|
{
|
||||||
|
EXTEND(true), RETRACT(false);
|
||||||
|
|
||||||
|
public final boolean isExtend;
|
||||||
|
|
||||||
|
PistonMoveType(boolean isExtend)
|
||||||
|
{
|
||||||
|
this.isExtend = isExtend;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,154 @@
|
||||||
|
/*
|
||||||
|
* Minecraft Forge
|
||||||
|
* Copyright (c) 2016-2019.
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation version 2.1
|
||||||
|
* of the License.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.minecraftforge.debug.block;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import net.minecraft.block.Block;
|
||||||
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.block.Blocks;
|
||||||
|
import net.minecraft.block.PistonBlock;
|
||||||
|
import net.minecraft.block.material.Material;
|
||||||
|
import net.minecraft.block.state.PistonBlockStructureHelper;
|
||||||
|
import net.minecraft.client.Minecraft;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.item.BlockItem;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
|
import net.minecraft.item.ItemGroup;
|
||||||
|
import net.minecraft.util.Direction;
|
||||||
|
import net.minecraft.util.ResourceLocation;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.util.text.StringTextComponent;
|
||||||
|
import net.minecraft.world.World;
|
||||||
|
import net.minecraftforge.event.RegistryEvent;
|
||||||
|
import net.minecraftforge.event.world.PistonEvent;
|
||||||
|
import net.minecraftforge.event.world.PistonEvent.PistonMoveType;
|
||||||
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
|
import net.minecraftforge.fml.common.Mod;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This test mod blocks pistons from moving cobblestone at all except indirectly
|
||||||
|
* This test mod adds a block that moves upwards when pushed by a piston
|
||||||
|
* This test mod informs the user what will happen the piston and affected blocks when changes are made
|
||||||
|
* This test mod makes black wool pushed by a piston drop after being pushed.
|
||||||
|
*/
|
||||||
|
@Mod.EventBusSubscriber(modid = PistonEventTest.MODID)
|
||||||
|
@Mod(value = PistonEventTest.MODID)
|
||||||
|
public class PistonEventTest
|
||||||
|
{
|
||||||
|
|
||||||
|
public static final String MODID = "pistoneventtest";
|
||||||
|
|
||||||
|
private static Block shiftOnMove = new BlockShiftOnMove();
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
public static void regItem(RegistryEvent.Register<Item> event)
|
||||||
|
{
|
||||||
|
event.getRegistry().register(new BlockItem(shiftOnMove, new Item.Properties().group(ItemGroup.BUILDING_BLOCKS)).setRegistryName(shiftOnMove.getRegistryName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
public static void regBlock(RegistryEvent.Register<Block> event)
|
||||||
|
{
|
||||||
|
event.getRegistry().register(shiftOnMove);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
public static void pistonPre(PistonEvent.Pre event)
|
||||||
|
{
|
||||||
|
if (event.getPistonMoveType() == PistonMoveType.EXTEND)
|
||||||
|
{
|
||||||
|
World world = (World) event.getWorld();
|
||||||
|
PistonBlockStructureHelper pistonHelper = event.getStructureHelper();
|
||||||
|
if (world.isRemote)
|
||||||
|
{
|
||||||
|
PlayerEntity player = Minecraft.getInstance().player;
|
||||||
|
if (pistonHelper.canMove())
|
||||||
|
{
|
||||||
|
player.sendMessage(new StringTextComponent(String.format("Piston will extend moving %d blocks and destroy %d blocks", pistonHelper.getBlocksToMove().size(), pistonHelper.getBlocksToDestroy().size())));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
player.sendMessage(new StringTextComponent("Piston won't extend"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pistonHelper.canMove())
|
||||||
|
{
|
||||||
|
List<BlockPos> posList = pistonHelper.getBlocksToMove();
|
||||||
|
for (BlockPos newPos : posList)
|
||||||
|
{
|
||||||
|
BlockState state = event.getWorld().getBlockState(newPos);
|
||||||
|
if (state.getBlock() == Blocks.BLACK_WOOL)
|
||||||
|
{
|
||||||
|
Block.spawnDrops(state, world, newPos);
|
||||||
|
world.setBlockState(newPos, Blocks.AIR.getDefaultState());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make the block move up and out of the way so long as it won't replace the piston
|
||||||
|
BlockPos pushedBlockPos = event.getFaceOffsetPos();
|
||||||
|
if (world.getBlockState(pushedBlockPos).getBlock() == shiftOnMove && event.getDirection() != Direction.DOWN)
|
||||||
|
{
|
||||||
|
world.setBlockState(pushedBlockPos, Blocks.AIR.getDefaultState());
|
||||||
|
world.setBlockState(pushedBlockPos.up(), shiftOnMove.getDefaultState());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Block pushing cobblestone (directly, indirectly works)
|
||||||
|
event.setCanceled(event.getWorld().getBlockState(event.getFaceOffsetPos()).getBlock() == Blocks.COBBLESTONE);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
boolean isSticky = event.getWorld().getBlockState(event.getPos()).getBlock() == Blocks.STICKY_PISTON;
|
||||||
|
if (event.getWorld().isRemote())
|
||||||
|
{
|
||||||
|
PlayerEntity player = Minecraft.getInstance().player;
|
||||||
|
if (isSticky)
|
||||||
|
{
|
||||||
|
BlockPos targetPos = event.getFaceOffsetPos().offset(event.getDirection());
|
||||||
|
boolean canPush = PistonBlock.canPush(event.getWorld().getBlockState(targetPos), (World) event.getWorld(), event.getFaceOffsetPos(), event.getDirection().getOpposite(), false, event.getDirection());
|
||||||
|
boolean isAir = event.getWorld().isAirBlock(targetPos);
|
||||||
|
player.sendMessage(new StringTextComponent(String.format("Piston will retract moving %d blocks", !isAir && canPush ? 1 : 0)));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
player.sendMessage(new StringTextComponent("Piston will retract"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Offset twice to see if retraction will pull cobblestone
|
||||||
|
event.setCanceled(event.getWorld().getBlockState(event.getFaceOffsetPos().offset(event.getDirection())).getBlock() == Blocks.COBBLESTONE && isSticky);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class BlockShiftOnMove extends Block
|
||||||
|
{
|
||||||
|
public static String blockName = "shiftonmove";
|
||||||
|
public static ResourceLocation blockId = new ResourceLocation(MODID, blockName);
|
||||||
|
|
||||||
|
public BlockShiftOnMove()
|
||||||
|
{
|
||||||
|
super(Block.Properties.create(Material.ROCK));
|
||||||
|
this.setRegistryName(blockId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue