-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
81 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/com/cstav/genshinstrument/event/ServerEvents.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.cstav.genshinstrument.event; | ||
|
||
import com.cstav.genshinstrument.GInstrumentMod; | ||
import com.cstav.genshinstrument.capability.instrumentOpen.InstrumentOpenProvider; | ||
import com.cstav.genshinstrument.item.InstrumentItem; | ||
import com.cstav.genshinstrument.util.CommonUtil; | ||
import com.cstav.genshinstrument.util.ServerUtil; | ||
|
||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraftforge.event.TickEvent.LevelTickEvent; | ||
import net.minecraftforge.event.TickEvent.Phase; | ||
import net.minecraftforge.eventbus.api.SubscribeEvent; | ||
import net.minecraftforge.fml.LogicalSide; | ||
import net.minecraftforge.fml.common.Mod.EventBusSubscriber; | ||
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus; | ||
|
||
@EventBusSubscriber(bus = Bus.FORGE, modid = GInstrumentMod.MODID) | ||
public abstract class ServerEvents { | ||
|
||
@SubscribeEvent | ||
public static void onServerTick(final LevelTickEvent event) { | ||
if ((event.phase != Phase.END) && (event.side == LogicalSide.SERVER)) | ||
event.level.players().forEach(ServerEvents::handleAbruptInstrumentClose); | ||
} | ||
|
||
private static void handleAbruptInstrumentClose(final Player player) { | ||
if (!InstrumentOpenProvider.isOpen(player)) | ||
return; | ||
|
||
if (InstrumentOpenProvider.isItem(player)) { | ||
// This is done like so because there is no event (that I know of) for when an item is moved/removed | ||
if (CommonUtil.getItemInHands(InstrumentItem.class, player).isEmpty()) | ||
ServerUtil.setInstrumentClosed(player); | ||
} else { | ||
// Close an instrument block if the player is too far away | ||
if (!InstrumentOpenProvider.getBlockPos(player).closerToCenterThan(player.position(), 6)) | ||
ServerUtil.setInstrumentClosed(player); | ||
} | ||
} | ||
|
||
} |
20 changes: 19 additions & 1 deletion
20
src/main/java/com/cstav/genshinstrument/util/CommonUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters