Skip to content

Commit

Permalink
feat: Add gun aiming and shooting
Browse files Browse the repository at this point in the history
  • Loading branch information
jimchen5209 committed Jan 7, 2024
1 parent 9920ceb commit c43de8f
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2022
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.mixin.event;

import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import one.oktw.galaxy.Main;
import one.oktw.galaxy.event.type.PlayerDropItemEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ServerPlayNetworkHandler.class)
public class MixinPlayerDropItem_NetworkHandler {
@Shadow
public ServerPlayerEntity player;

@Inject(
method = "onPlayerAction",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/network/ServerPlayerEntity;dropSelectedItem(Z)Z",
shift = At.Shift.BEFORE
), cancellable = true)
private void onPlayerAction(PlayerActionC2SPacket packet, CallbackInfo ci) {
Main main = Main.Companion.getMain();
if (main != null && main.getEventManager().emit(new PlayerDropItemEvent(player)).getCancel()) {
ci.cancel();
player.currentScreenHandler.syncState();
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2022
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.mixin.event;

import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import one.oktw.galaxy.Main;
import one.oktw.galaxy.event.type.PlayerPickupItemEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ItemEntity.class)
public class MixinPlayerPickupItem_ItemEntity {
@Inject(
method = "onPlayerCollision",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/entity/player/PlayerEntity;triggerItemPickedUpByEntityCriteria(Lnet/minecraft/entity/ItemEntity;)V",
shift = At.Shift.AFTER
))
private void onPlayerCollision(PlayerEntity player, CallbackInfo ci) {
Main main = Main.Companion.getMain();
if (main == null) return;
main.getEventManager().emit(new PlayerPickupItemEvent((ServerPlayerEntity) player));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2021
* Copyright (C) 2018-2022
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
Expand All @@ -18,31 +18,27 @@

package one.oktw.galaxy.mixin.event;

import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import one.oktw.galaxy.Main;
import one.oktw.galaxy.event.type.HotBarSlotUpdateEvent;
import one.oktw.galaxy.event.type.PlayerSwapItemInHandEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(targets = {"net/minecraft/server/network/ServerPlayerEntity$2"})
public class MixinHotBarSlotUpdate_ScreenHandlerListener {
@Shadow(aliases = {"field_29183"})
private ServerPlayerEntity player;
@Mixin(ServerPlayNetworkHandler.class)
public class MixinPlayerSwapItemInHand_NetworkHandler {
@Shadow
public ServerPlayerEntity player;

@Inject(
method = "onSlotUpdate(Lnet/minecraft/screen/ScreenHandler;ILnet/minecraft/item/ItemStack;)V",
at = @At(value = "RETURN")
)
private void onSlotUpdate(ScreenHandler handler, int slotId, ItemStack stack, CallbackInfo ci) {
@Inject(method = "onPlayerAction", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;clearActiveItem()V", shift = At.Shift.AFTER))
private void onPlayerAction(PlayerActionC2SPacket packet, CallbackInfo ci) {
Main main = Main.Companion.getMain();
if (main == null) return;
if (slotId >= 36 && slotId <= 45) {
main.getEventManager().emit(new HotBarSlotUpdateEvent(player, handler, slotId, stack));
}
main.getEventManager().emit(new PlayerSwapItemInHandEvent(player));
}

}
3 changes: 2 additions & 1 deletion src/main/kotlin/one/oktw/galaxy/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import one.oktw.galaxy.command.commands.Spawn
import one.oktw.galaxy.event.EventManager
import one.oktw.galaxy.event.type.ProxyResponseEvent
import one.oktw.galaxy.item.event.CustomItemEventHandler
import one.oktw.galaxy.item.event.Gun
import one.oktw.galaxy.item.event.Wrench
import one.oktw.galaxy.player.Harvest
import one.oktw.galaxy.proxy.api.ProxyAPI
Expand Down Expand Up @@ -99,7 +100,7 @@ class Main : DedicatedServerModInitializer, CoroutineScope {
eventManager.register(Elevator())
eventManager.register(AngelBlock())
eventManager.register(CustomItemEventHandler())
// eventManager.register(Weapon())
eventManager.register(Gun())
})

ServerLifecycleEvents.SERVER_STOPPING.register {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2021
* Copyright (C) 2018-2022
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
Expand All @@ -18,8 +18,6 @@

package one.oktw.galaxy.event.type

import net.minecraft.item.ItemStack
import net.minecraft.screen.ScreenHandler
import net.minecraft.server.network.ServerPlayerEntity

class HotBarSlotUpdateEvent(val player: ServerPlayerEntity, val handler: ScreenHandler, val slotId: Int, val item: ItemStack) : Event
class PlayerDropItemEvent(val player: ServerPlayerEntity) : CancelableEvent()
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2022
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.event.type

import net.minecraft.server.network.ServerPlayerEntity

class PlayerPickupItemEvent(val player: ServerPlayerEntity) : Event
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* OKTW Galaxy Project
* Copyright (C) 2018-2022
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package one.oktw.galaxy.event.type

import net.minecraft.server.network.ServerPlayerEntity

class PlayerSwapItemInHandEvent(val player: ServerPlayerEntity) : Event
Loading

0 comments on commit c43de8f

Please sign in to comment.