Skip to content

Commit

Permalink
Merge pull request #130 from gnembon/events_and_schedules
Browse files Browse the repository at this point in the history
Events and schedules
  • Loading branch information
gnembon authored Jun 1, 2019
2 parents 6bdd594 + e0132a4 commit d9d4ba0
Show file tree
Hide file tree
Showing 12 changed files with 707 additions and 64 deletions.
102 changes: 98 additions & 4 deletions patches/net/minecraft/network/NetHandlerPlayServer.java.patch
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
--- a/net/minecraft/network/NetHandlerPlayServer.java
+++ b/net/minecraft/network/NetHandlerPlayServer.java
@@ -1,5 +1,7 @@
@@ -1,5 +1,8 @@
package net.minecraft.network;

+import carpet.CarpetServer;
+import carpet.CarpetSettings;
+import carpet.helpers.TickSpeed;
import com.google.common.primitives.Doubles;
import com.google.common.primitives.Floats;
import com.google.common.util.concurrent.Futures;
@@ -296,6 +298,11 @@
@@ -296,6 +299,12 @@
{
PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.player.getServerWorld());
this.player.setEntityActionState(packetIn.getStrafeSpeed(), packetIn.getForwardSpeed(), packetIn.isJumping(), packetIn.isSneaking());
+ //CM checking if player is moving, for commandTick
+ if (packetIn.getStrafeSpeed() != 0.0F || packetIn.getForwardSpeed() != 0.0F || packetIn.isJumping() || packetIn.isSneaking())
+ {
+ CarpetServer.scriptServer.events.onMountControls(player, packetIn.getStrafeSpeed(), packetIn.getForwardSpeed(), packetIn.isJumping(), packetIn.isSneaking());
+ TickSpeed.reset_player_active_timeout();
+ }
}

private static boolean isMovePlayerPacketInvalid(CPacketPlayer packetIn)
@@ -831,6 +838,12 @@
@@ -831,6 +840,12 @@
double d10 = this.player.motionX * this.player.motionX + this.player.motionY * this.player.motionY + this.player.motionZ * this.player.motionZ;
double d11 = d7 * d7 + d8 * d8 + d9 * d9;

Expand All @@ -33,7 +35,7 @@
if (this.player.isPlayerSleeping())
{
if (d11 > 1.0D)
@@ -849,7 +862,8 @@
@@ -849,7 +864,8 @@
i = 1;
}

Expand All @@ -43,3 +45,95 @@
{
float f2 = this.player.isElytraFlying() ? 300.0F : 100.0F;

@@ -869,6 +885,8 @@
if (this.player.onGround && !packetIn.isOnGround() && d8 > 0.0D)
{
this.player.jump();
+ //CM
+ CarpetServer.scriptServer.events.onPlayerJumped(player);
}

this.player.move(MoverType.PLAYER, d7, d8, d9);
@@ -1006,6 +1024,9 @@
{
if (!this.server.isBlockProtected(worldserver, blockpos, this.player) && worldserver.getWorldBorder().contains(blockpos))
{
+ // CM event hook
+ CarpetServer.scriptServer.events.onBlockClicked(player, blockpos, packetIn.getFacing());
+ //
this.player.interactionManager.startDestroyBlock(blockpos, packetIn.getFacing());
}
else
@@ -1052,6 +1073,8 @@
{
if (this.targetPos == null && this.player.getDistanceSq((double)blockpos.getX() + 0.5D, (double)blockpos.getY() + 0.5D, (double)blockpos.getZ() + 0.5D) < 64.0D && !this.server.isBlockProtected(worldserver, blockpos, this.player) && worldserver.getWorldBorder().contains(blockpos))
{
+ //CM
+ CarpetServer.scriptServer.events.onRightClickBlock(this.player, itemstack, enumhand, blockpos, enumfacing, new Vec3d( packetIn.getFacingX(), packetIn.getFacingY(), packetIn.getFacingZ()));
this.player.interactionManager.processRightClickBlock(this.player, worldserver, itemstack, enumhand, blockpos, enumfacing, packetIn.getFacingX(), packetIn.getFacingY(), packetIn.getFacingZ());
}
}
@@ -1073,6 +1096,9 @@
ItemStack itemstack = this.player.getHeldItem(enumhand);
this.player.markPlayerActive();

+ //CM
+ CarpetServer.scriptServer.events.onRightClick(player, itemstack, enumhand);
+
if (!itemstack.isEmpty())
{
this.player.interactionManager.processRightClick(this.player, worldserver, itemstack, enumhand);
@@ -1250,19 +1276,25 @@
switch (packetIn.getAction())
{
case START_SNEAKING:
+ CarpetServer.scriptServer.events.onStartSneaking(player);
this.player.setSneaking(true);
break;
case STOP_SNEAKING:
+ CarpetServer.scriptServer.events.onStopSneaking(player);
this.player.setSneaking(false);
break;
case START_SPRINTING:
+ CarpetServer.scriptServer.events.onStartSprinting(player);
this.player.setSprinting(true);
break;
case STOP_SPRINTING:
+ CarpetServer.scriptServer.events.onStopSprinting(player);
this.player.setSprinting(false);
break;
case STOP_SLEEPING:

+ CarpetServer.scriptServer.events.onOutOfBed(player);
+
if (this.player.isPlayerSleeping())
{
this.player.wakeUpPlayer(false, true, true);
@@ -1303,6 +1335,8 @@
break;
case START_FALL_FLYING:

+ CarpetServer.scriptServer.events.onElytraDeploy(player);
+
if (!this.player.onGround && this.player.motionY < 0.0D && !this.player.isElytraFlying() && !this.player.isInWater())
{
ItemStack itemstack = this.player.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
@@ -1345,6 +1379,9 @@
if (packetIn.getAction() == CPacketUseEntity.Action.INTERACT)
{
EnumHand enumhand = packetIn.getHand();
+ //
+ CarpetServer.scriptServer.events.onEntityInteracted(player, entity, enumhand);
+ //
this.player.interactOn(entity, enumhand);
}
else if (packetIn.getAction() == CPacketUseEntity.Action.INTERACT_AT)
@@ -1360,6 +1397,8 @@
this.server.logWarning("Player " + this.player.getName().getString() + " tried to attack an invalid entity");
return;
}
+ //
+ CarpetServer.scriptServer.events.onEntityAttacked(player, entity);

this.player.attackTargetEntityWithCurrentItem(entity);
}
16 changes: 12 additions & 4 deletions patches/net/minecraft/server/MinecraftServer.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@
}

this.profiler.startSection("snooper");
@@ -834,6 +871,10 @@
@@ -829,11 +866,18 @@
}

this.profiler.endSection();
+ //CM scarpet events
+ CarpetServer.scriptServer.events.onTick();
+ //
this.profiler.startSection("tallying");
long l = this.tickTimeArray[this.tickCounter % 100] = Util.nanoTime() - i;
this.tickTime = this.tickTime * 0.8F + (float)l / 1000000.0F * 0.19999999F;
this.profiler.endSection();
this.profiler.endSection();
Expand All @@ -122,7 +130,7 @@
}

public void updateTimeLightAndEntities(BooleanSupplier hasTimeLeft)
@@ -905,10 +946,12 @@
@@ -905,10 +949,12 @@
}))[this.tickCounter % 100] = Util.nanoTime() - i;
}

Expand All @@ -135,7 +143,7 @@
this.profiler.endStartSection("tickables");

for (int j = 0; j < this.tickables.size(); ++j)
@@ -1605,6 +1648,12 @@
@@ -1605,6 +1651,12 @@
{
return this.serverTime;
}
Expand All @@ -148,7 +156,7 @@

public Thread getServerThread()
{
@@ -1825,4 +1874,12 @@
@@ -1825,4 +1877,12 @@
return 0;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
--- a/net/minecraft/server/management/PlayerInteractionManager.java
+++ b/net/minecraft/server/management/PlayerInteractionManager.java
@@ -1,5 +1,6 @@
@@ -1,5 +1,7 @@
package net.minecraft.server.management;

+import carpet.CarpetServer;
+import carpet.helpers.BlockRotator;
import net.minecraft.block.Block;
import net.minecraft.block.BlockChest;
import net.minecraft.block.BlockCommandBlock;
@@ -401,11 +402,20 @@
@@ -241,6 +243,12 @@
{
iblockstate.getBlock().onPlayerDestroy(this.world, pos, iblockstate);
}
+ //CM
+ if(flag)
+ {
+ CarpetServer.scriptServer.events.onBlockBroken(player, world, pos, iblockstate);
+ }
+ // end CM

return flag;
}
@@ -401,11 +409,20 @@
boolean flag = !player.getHeldItemMainhand().isEmpty() || !player.getHeldItemOffhand().isEmpty();
boolean flag1 = player.isSneaking() && flag;

Expand Down
1 change: 1 addition & 0 deletions src/main/java/carpet/CarpetServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static void tick(MinecraftServer server)
{
TickSpeed.tick(server);
HUDController.update_hud(server);
scriptServer.events.tick(); // in 1.14 make sure its called in the aftertick
}

public static void registerCarpetCommands(CommandDispatcher<CommandSource> dispatcher)
Expand Down
54 changes: 53 additions & 1 deletion src/main/java/carpet/commands/ScriptCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import carpet.CarpetServer;
import carpet.CarpetSettings;
import carpet.script.CarpetEventServer;
import carpet.script.CarpetExpression;
import carpet.script.Expression;
import carpet.script.ExpressionInspector;
Expand Down Expand Up @@ -191,9 +192,42 @@ public static void register(CommandDispatcher<CommandSource> dispatcher)
return success?1:0;
}));

LiteralArgumentBuilder<CommandSource> q = literal("event").requires( (player) -> player.hasPermissionLevel(2) ).
executes( (cc) -> listEvents(cc.getSource())).
then(literal("add_to").
then(argument("event", StringArgumentType.word()).
suggests( (cc, bb) -> suggest(CarpetServer.scriptServer.events.eventHandlers.keySet() ,bb)).
then(argument("call", StringArgumentType.word()).
suggests( (cc, bb) -> suggest(suggestFunctionCalls(cc), bb)).
executes( (cc) -> CarpetServer.scriptServer.events.addEvent(
StringArgumentType.getString(cc, "event"),
null,
StringArgumentType.getString(cc, "call")
)?1:0)).
then(literal("from").
then(argument("package", StringArgumentType.word()).
suggests( (cc, bb) -> suggest(CarpetServer.scriptServer.modules.keySet(), bb)).
then(argument("call", StringArgumentType.word()).
suggests( (cc, bb) -> suggest(suggestFunctionCalls(cc), bb)).
executes( (cc) -> CarpetServer.scriptServer.events.addEvent(
StringArgumentType.getString(cc, "event"),
StringArgumentType.getString(cc, "package"),
StringArgumentType.getString(cc, "call")
)?1:0)))))).
then(literal("remove_from").
then(argument("event", StringArgumentType.word()).
suggests( (cc, bb) -> suggest(CarpetServer.scriptServer.events.eventHandlers.keySet() ,bb)).
then(argument("call", StringArgumentType.greedyString()).
suggests( (cc, bb) -> suggest(CarpetServer.scriptServer.events.eventHandlers.get(StringArgumentType.getString(cc, "event")).callList.stream().map(CarpetEventServer.Callback::toString), bb)).
executes( (cc) -> CarpetServer.scriptServer.events.removeEvent(
StringArgumentType.getString(cc, "event"),
StringArgumentType.getString(cc, "call")
)?1:0))));


dispatcher.register(literal("script").
requires((player) -> CarpetSettings.getBool("commandScript")).
then(b).then(u).then(o).then(l).then(s).then(c).then(h).then(i).then(e).then(t).then(a).then(f));
then(b).then(u).then(o).then(l).then(s).then(c).then(h).then(i).then(e).then(t).then(a).then(f).then(q));
dispatcher.register(literal("script").
requires((player) -> CarpetSettings.getBool("commandScript")).
then(literal("in").
Expand All @@ -220,6 +254,24 @@ private static Collection<String> suggestFunctionCalls(CommandContext<CommandSou
ScriptHost host = getHost(c);
return host.getPublicFunctions();
}
private static int listEvents(CommandSource source)
{
Messenger.m(source, "w Lists ALL event handlers:");
for (String event: CarpetServer.scriptServer.events.eventHandlers.keySet())
{
boolean shownEvent = false;
for (CarpetEventServer.Callback c: CarpetServer.scriptServer.events.eventHandlers.get(event).callList)
{
if (!shownEvent)
{
Messenger.m(source, "w Handlers for "+event+": ");
shownEvent = true;
}
Messenger.m(source, "w - "+c.udf+(c.host==null?"":" (from "+c.host+")"));
}
}
return 1;
}
private static int listGlobals(CommandContext<CommandSource> context)
{
ScriptHost host = getHost(context);
Expand Down
Loading

0 comments on commit d9d4ba0

Please sign in to comment.