Skip to content

Commit

Permalink
add block break and place events, move to folder
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueWeabo committed Jul 19, 2024
1 parent cca8aa5 commit 7a5e57c
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import com.gtnewhorizons.mutecore.api.registry.EventRegistry;
import com.gtnewhorizons.mutecore.api.registry.MultiTileContainer.Id;
import com.gtnewhorizons.mutecore.api.registry.MultiTileEntityRegistry;
import com.gtnewhorizons.mutecore.api.registry.NeighborBlockChangeEvent;
import com.gtnewhorizons.mutecore.api.registry.NeighborTileChangeEvent;
import com.gtnewhorizons.mutecore.api.registry.PlayerInteractionEvent;
import com.gtnewhorizons.mutecore.api.event.BlockBreakEvent;
import com.gtnewhorizons.mutecore.api.event.BlockPlaceEvent;
import com.gtnewhorizons.mutecore.api.event.NeighborBlockChangeEvent;
import com.gtnewhorizons.mutecore.api.event.NeighborTileChangeEvent;
import com.gtnewhorizons.mutecore.api.event.PlayerInteractionEvent;
import com.gtnewhorizons.mutecore.api.tile.MultiTileEntity;
import com.gtnewhorizons.mutecore.client.MultiTileBlockRenderer;

Expand Down Expand Up @@ -65,16 +67,10 @@ public void onNeighborBlockChange(World worldIn, int x, int y, int z, Block neig
super.onNeighborBlockChange(worldIn, x, y, z, neighbor);
TileEntity te = worldIn.getTileEntity(x, y, z);
if (!(te instanceof MultiTileEntity mute)) return;
Object eventComponent = null;
Entity entity = mute.getEntity();
for (NeighborBlockChangeEvent preEvent : EventRegistry.NEIGHBOR_BLOCK_CHANGE_EVENTS) {
eventComponent = preEvent.generate(neighbor, x, y, z, mute.getEntity());
if (eventComponent != null) break;
preEvent.call(neighbor, x, y, z, entity);
}
if (eventComponent == null) return;
if (mute.getEntity()
.has(eventComponent.getClass())) return;
mute.getEntity()
.add(eventComponent);
}

@Override
Expand All @@ -83,16 +79,10 @@ public void onNeighborChange(IBlockAccess world, int x, int y, int z, int tileX,
TileEntity te = world.getTileEntity(x, y, z);
TileEntity changed = world.getTileEntity(tileX, tileY, tileZ);
if (!(te instanceof MultiTileEntity mute)) return;
Object eventComponent = null;
Entity entity = mute.getEntity();
for (NeighborTileChangeEvent preEvent : EventRegistry.NEIGHBOR_TILE_CHANGE_EVENTS) {
eventComponent = preEvent.generate(changed, mute.getEntity());
if (eventComponent != null) break;
preEvent.call(changed, entity);
}
if (eventComponent == null) return;
if (mute.getEntity()
.has(eventComponent.getClass())) return;
mute.getEntity()
.add(eventComponent);
}

@Override
Expand Down Expand Up @@ -124,7 +114,11 @@ public boolean onBlockActivated(World worldIn, int x, int y, int z, EntityPlayer
public void breakBlock(World worldIn, int x, int y, int z, Block blockBroken, int meta) {
TileEntity te = worldIn.getTileEntity(x, y, z);
if (!(te instanceof MultiTileEntity mute)) return;
MuTECore.ENGINE.deleteEntity(mute.getEntity());
Entity entity = mute.getEntity();
for (BlockBreakEvent preEvent : EventRegistry.BLOCK_BREAK_EVENTS) {
preEvent.call(entity);
}
MuTECore.ENGINE.deleteEntity(entity);
super.breakBlock(worldIn, x, y, z, blockBroken, meta);
}

Expand All @@ -133,8 +127,11 @@ public void onBlockAdded(World worldIn, int x, int y, int z) {
super.onBlockAdded(worldIn, x, y, z);
TileEntity te = worldIn.getTileEntity(x, y, z);
if (!(te instanceof MultiTileEntity mute)) return;
mute.getEntity()
.add(new Coordinates(x, y, z));
Entity entity = mute.getEntity();
entity.add(new Coordinates(x, y, z));
for (BlockPlaceEvent preEvent : EventRegistry.BLOCK_PLACE_EVENTS) {
preEvent.call(entity);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.gtnewhorizons.mutecore.api.event;

import javax.annotation.Nonnull;

import dev.dominion.ecs.api.Entity;

public class BlockBreakEvent implements Comparable<BlockBreakEvent>{

private int priority;
private BlockBreak interaction;

public BlockBreakEvent(int priority, @Nonnull BlockBreak interaction) {
this.priority = priority;
this.interaction = interaction;
}

public final @Nonnull void call(Entity entity) {
interaction.executeEvent(entity);
}

@FunctionalInterface
public static interface BlockBreak {

@Nonnull
void executeEvent(@Nonnull Entity entity);
}

@Override
public int compareTo(@Nonnull BlockBreakEvent other) {
return Integer.compare(priority, other.priority);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.gtnewhorizons.mutecore.api.event;

import javax.annotation.Nonnull;

import dev.dominion.ecs.api.Entity;

public class BlockPlaceEvent implements Comparable<BlockPlaceEvent>{

private int priority;
private BlockPlace interaction;

public BlockPlaceEvent(int priority, @Nonnull BlockPlace interaction) {
this.priority = priority;
this.interaction = interaction;
}

public final @Nonnull void call(Entity entity) {
interaction.executeEvent(entity);
}

@FunctionalInterface
public static interface BlockPlace {

@Nonnull
void executeEvent(@Nonnull Entity entity);
}

@Override
public int compareTo(@Nonnull BlockPlaceEvent other) {
return Integer.compare(priority, other.priority);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gtnewhorizons.mutecore.api.registry;
package com.gtnewhorizons.mutecore.api.event;

import javax.annotation.Nonnull;

Expand All @@ -15,15 +15,15 @@ public NeighborBlockChangeEvent(int priority, @Nonnull NeighborBlockChange inter
this.interaction = interaction;
}

public final @Nonnull Object generate(@Nonnull Block neighbor, int x, int y, int z, @Nonnull Entity entity) {
return interaction.generateComponent(neighbor, x, y, z, entity);
public final @Nonnull void call(@Nonnull Block neighbor, int x, int y, int z, @Nonnull Entity entity) {
interaction.executeEvent(neighbor, x, y, z, entity);
}

@FunctionalInterface
public static interface NeighborBlockChange {

@Nonnull
Object generateComponent(@Nonnull Block neighbor, int x, int y, int z, @Nonnull Entity entity);
void executeEvent(@Nonnull Block neighbor, int x, int y, int z, @Nonnull Entity entity);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gtnewhorizons.mutecore.api.registry;
package com.gtnewhorizons.mutecore.api.event;

import javax.annotation.Nonnull;

Expand All @@ -15,15 +15,15 @@ public NeighborTileChangeEvent(int priority, @Nonnull NeighborTileChange interac
this.interaction = interaction;
}

public final @Nonnull Object generate(@Nonnull TileEntity neighbor, @Nonnull Entity entity) {
return interaction.generateComponent(neighbor, entity);
public final @Nonnull void call(@Nonnull TileEntity neighbor, @Nonnull Entity entity) {
interaction.executeEvent(neighbor, entity);
}

@FunctionalInterface
public static interface NeighborTileChange {

@Nonnull
Object generateComponent(@Nonnull TileEntity neighbor, @Nonnull Entity entity);
void executeEvent(@Nonnull TileEntity neighbor, @Nonnull Entity entity);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gtnewhorizons.mutecore.api.registry;
package com.gtnewhorizons.mutecore.api.event;

import javax.annotation.Nonnull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@

import org.jetbrains.annotations.ApiStatus.Internal;

import com.gtnewhorizons.mutecore.api.event.BlockBreakEvent;
import com.gtnewhorizons.mutecore.api.event.BlockPlaceEvent;
import com.gtnewhorizons.mutecore.api.event.NeighborBlockChangeEvent;
import com.gtnewhorizons.mutecore.api.event.NeighborTileChangeEvent;
import com.gtnewhorizons.mutecore.api.event.PlayerInteractionEvent;

public class EventRegistry {

public static final @Internal @Nonnull List<PlayerInteractionEvent> PLAYER_INTERACTION_EVENTS = new ArrayList<>();
public static final @Internal @Nonnull List<NeighborBlockChangeEvent> NEIGHBOR_BLOCK_CHANGE_EVENTS = new ArrayList<>();
public static final @Internal @Nonnull List<NeighborTileChangeEvent> NEIGHBOR_TILE_CHANGE_EVENTS = new ArrayList<>();
public static final @Internal @Nonnull List<BlockBreakEvent> BLOCK_BREAK_EVENTS = new ArrayList<>();
public static final @Internal @Nonnull List<BlockPlaceEvent> BLOCK_PLACE_EVENTS = new ArrayList<>();

/**
* Register a player interaction event. This is not the processing of the event,
* Register a player interaction event processor. This is not the processing of the event,
* but the way to delegate it to said processing.
*/
public static void registerPlayerInteractionEvent(@Nonnull PlayerInteractionEvent event) {
Expand All @@ -24,20 +32,34 @@ public static void registerPlayerInteractionEvent(@Nonnull PlayerInteractionEven
}

/**
* Register a neighbor block change event. This is not the processing of the event,
* but the way to delegate it to said processing.
* Register a neighbor block change event processor. This does the actual processing for the event.
*/
public static void registerNeighborBlockChangeEvent(@Nonnull NeighborBlockChangeEvent event) {
NEIGHBOR_BLOCK_CHANGE_EVENTS.add(event);
NEIGHBOR_BLOCK_CHANGE_EVENTS.sort(Comparator.reverseOrder());
}

/**
* Register a neighbor tile change event. This is not the processing of the event,
* but the way to delegate it to said processing.
* Register a neighbor tile change event processor. This does the actual processing for the event.
*/
public static void registerNeighborTileChangeEvent(@Nonnull NeighborTileChangeEvent event) {
NEIGHBOR_TILE_CHANGE_EVENTS.add(event);
NEIGHBOR_TILE_CHANGE_EVENTS.sort(Comparator.reverseOrder());
}

/**
* Register a block break event processor. This does the actual processing for the event.
*/
public static void registerBlockBreakEvent(@Nonnull BlockBreakEvent event) {
BLOCK_BREAK_EVENTS.add(event);
BLOCK_BREAK_EVENTS.sort(Comparator.reverseOrder());
}

/**
* Register a block place event processor. This does the actual processing for the event.
*/
public static void registerBlockPlaceEvent(@Nonnull BlockPlaceEvent event) {
BLOCK_PLACE_EVENTS.add(event);
BLOCK_PLACE_EVENTS.sort(Comparator.reverseOrder());
}
}

0 comments on commit 7a5e57c

Please sign in to comment.